summaryrefslogtreecommitdiff
path: root/examples/test-facebook.c
blob: dac21e1bb1ab1e777bbf7ff84848ef623c3e83e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#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, *name;

  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 some authenticated calls */

  /* Get the user name */
  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", "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");
  name = g_strdup (node->content);
  g_print ("Logged in as %s\n", name);
  rest_xml_node_unref (root);

  /* Get the user status messages */
  call = rest_proxy_new_call (proxy);
  rest_proxy_call_set_function (call, "status.get");
  rest_proxy_call_add_param (call, "limit", "5");

  if (!rest_proxy_call_run (call, NULL, NULL))
    g_error ("Cannot get statuses");

  root = get_xml (call);
  {
    RestXmlNode *node, *msg;
    for (node = rest_xml_node_find (root, "status"); node; node = node->next) {
      msg = rest_xml_node_find (node, "message");
      g_print ("%s %s\n", name, msg->content);
    }
  }
  rest_xml_node_unref (root);

  return 0;
}