diff options
author | Ross Burton <ross@linux.intel.com> | 2009-05-24 10:32:16 +0100 |
---|---|---|
committer | Ross Burton <ross@linux.intel.com> | 2009-05-24 21:25:01 +0100 |
commit | e6aff0f41fe2c9b9ec2a0351a36e0a772d7df750 (patch) | |
tree | 2d8f9e08c5d10236988232b394a19701d27e8362 | |
parent | d82692abfd3770b62e79f5295797f873129811c4 (diff) | |
download | librest-e6aff0f41fe2c9b9ec2a0351a36e0a772d7df750.tar.gz |
Add facebook test case
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | examples/Makefile.am | 3 | ||||
-rw-r--r-- | examples/test-facebook.c | 101 |
3 files changed, 105 insertions, 2 deletions
@@ -36,7 +36,8 @@ examples/test-raw examples/test-xml examples/dump-xml examples/test-flickr +examples/test-facebook tests/oauth tests/proxy tests/flickr -rest/test-runner
\ No newline at end of file +rest/test-runner diff --git a/examples/Makefile.am b/examples/Makefile.am index c857ce2..800488d 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -1,4 +1,4 @@ -noinst_PROGRAMS = test-raw test-xml test-oauth dump-xml test-flickr +noinst_PROGRAMS = test-raw test-xml test-oauth dump-xml test-flickr test-facebook AM_CFLAGS = $(GLIB_CFLAGS) $(SOUP_CFLAGS) -I$(top_srcdir) AM_LDFLAGS = $(GLIB_LIBS) $(SOUP_LIBS) ../rest/librest.la @@ -8,3 +8,4 @@ test_xml_SOURCES = test-xml.c test_oauth_SOURCES = test-oauth.c dump_xml_SOURCES = dump-xml.c test_flickr_SOURCES = test-flickr.c +test_facebook_SOURCES = test-facebook.c diff --git a/examples/test-facebook.c b/examples/test-facebook.c new file mode 100644 index 0000000..d4bcb8f --- /dev/null +++ b/examples/test-facebook.c @@ -0,0 +1,101 @@ +#include <stdio.h> +#include <string.h> +#include <rest/facebook-proxy.h> +#include <rest/rest-xml-parser.h> + +static RestXmlNode * +get_xml (RestProxyCall *call) +{ + static RestXmlParser *parser = NULL; + RestXmlNode *root; + + if (parser == NULL) + parser = rest_xml_parser_new (); + + root = rest_xml_parser_parse_from_data (parser, + rest_proxy_call_get_payload (call), + rest_proxy_call_get_payload_length (call)); + + if (strcmp (root->name,"error_response") == 0) { + RestXmlNode *node; + node = rest_xml_node_find (root, "error_msg"); + g_error ("Error from facebook: %s", node->content); + } + + g_object_unref (call); + + return root; +} + +int +main (int argc, char **argv) +{ + RestProxy *proxy; + RestProxyCall *call; + RestXmlNode *root, *node; + const char *secret, *session_key; + char *token, *url; + + g_thread_init (NULL); + g_type_init (); + + proxy = facebook_proxy_new ("9632214752c7dfb3a84890fbb6846dad", + "9dfdb14b9f110e0b14bb0607a14caefa"); + + if (argc == 3) { + facebook_proxy_set_app_secret (FACEBOOK_PROXY (proxy), argv[1]); + facebook_proxy_set_session_key (FACEBOOK_PROXY (proxy), argv[2]); + } else { + call = rest_proxy_new_call (proxy); + rest_proxy_call_set_function (call, "auth.createToken"); + + if (!rest_proxy_call_run (call, NULL, NULL)) + g_error ("Cannot get token"); + + root = get_xml (call); + if (strcmp (root->name, "auth_createToken_response") != 0) + g_error ("Unexpected response to createToken"); + + token = g_strdup (root->content); + rest_xml_node_unref (root); + + g_print ("Got token %s\n", token); + + url = facebook_proxy_build_login_url (FACEBOOK_PROXY (proxy), token); + + g_print ("Login URL %s\n", url); + + getchar (); + + call = rest_proxy_new_call (proxy); + rest_proxy_call_set_function (call, "auth.getSession"); + rest_proxy_call_add_param (call, "auth_token", token); + + if (!rest_proxy_call_run (call, NULL, NULL)) + g_error ("Cannot get token"); + + root = get_xml (call); + + session_key = rest_xml_node_find (root, "session_key")->content; + secret = rest_xml_node_find (root, "secret")->content; + g_print ("Got new secret %s and session key %s\n", secret, session_key); + + facebook_proxy_set_session_key (FACEBOOK_PROXY (proxy), session_key); + facebook_proxy_set_app_secret (FACEBOOK_PROXY (proxy), secret); + } + + /* Make an authenticated call */ + call = rest_proxy_new_call (proxy); + rest_proxy_call_set_function (call, "users.getInfo"); + rest_proxy_call_add_param (call, "uids", "1340627425"); + rest_proxy_call_add_param (call, "fields", "uid,name"); + + if (!rest_proxy_call_run (call, NULL, NULL)) + g_error ("Cannot get user info"); + + root = get_xml (call); + node = rest_xml_node_find (root, "name"); + g_print ("Logged in as %s\n", node->content); + + return 0; +} |