~ubuntu-branches/ubuntu/utopic/gnome-online-accounts/utopic

« back to all changes in this revision

Viewing changes to src/examples/add-pocket.c

  • Committer: Package Import Robot
  • Author(s): Andreas Henriksson, Laurent Bigonville, Andreas Henriksson
  • Date: 2014-05-16 11:42:52 UTC
  • mfrom: (1.1.28) (0.1.12 sid)
  • Revision ID: package-import@ubuntu.com-20140516114252-u5ect6mk6ht8i38x
Tags: 3.12.2-1
[ Laurent Bigonville ]
* debian/control.in: Recommends realmd package (Closes: #725965)

[ Andreas Henriksson ]
* New upstream release.
  - Removes chat support from Windows Live provider (XMPP gateway gone).
* Bump Standards-Version to 3.9.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 Bastien Nocera <hadess@hadess.net>
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Lesser General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General
 
15
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
 
16
 *
 
17
 * Author: Bastien Nocera <hadess@hadess.net>
 
18
 */
 
19
 
 
20
#include <config.h>
 
21
 
 
22
#define GOA_API_IS_SUBJECT_TO_CHANGE
 
23
#include <goa/goa.h>
 
24
 
 
25
#include <rest/rest-proxy.h>
 
26
#include <rest/rest-proxy-call.h>
 
27
 
 
28
int
 
29
main (int argc, char **argv)
 
30
{
 
31
  GError *error = NULL;
 
32
  GoaClient *client;
 
33
  GList *accounts, *l;
 
34
  GoaAccount *account = NULL;
 
35
  GoaOAuth2Based *oauth2 = NULL;
 
36
  char *access_token = NULL;
 
37
  RestProxy *proxy;
 
38
  RestProxyCall *call;
 
39
  const char *url;
 
40
  const char *tweet_id = NULL;
 
41
 
 
42
  if (argc != 2 && argc != 3) {
 
43
    g_print ("Usage: %s URL [TWEET ID]\n", argv[0]);
 
44
    return 1;
 
45
  }
 
46
  url = argv[1];
 
47
  if (argv[2] != NULL)
 
48
    tweet_id = argv[2];
 
49
 
 
50
  client = goa_client_new_sync (NULL, &error);
 
51
  if (!client) {
 
52
    g_error ("Could not create GoaClient: %s", error->message);
 
53
    return 1;
 
54
  }
 
55
 
 
56
  accounts = goa_client_get_accounts (client);
 
57
 
 
58
  /* Find a Pocket account */
 
59
  for (l = accounts; l != NULL; l = l->next) {
 
60
    GoaObject *object = GOA_OBJECT (l->data);
 
61
 
 
62
    account = goa_object_peek_account (object);
 
63
    if (g_strcmp0 (goa_account_get_provider_type (account), "pocket") == 0) {
 
64
      g_object_ref (account);
 
65
      oauth2 = goa_object_get_oauth2_based (object);
 
66
      break;
 
67
    }
 
68
  }
 
69
 
 
70
  g_list_free_full (accounts, (GDestroyNotify) g_object_unref);
 
71
 
 
72
  g_assert (account);
 
73
  g_assert (oauth2);
 
74
 
 
75
  if (!goa_oauth2_based_call_get_access_token_sync (oauth2, &access_token, NULL, NULL, &error)) {
 
76
    g_error ("Could not get access token: %s", error->message);
 
77
    return 1;
 
78
  }
 
79
 
 
80
  g_print ("Got access tokens\n");
 
81
 
 
82
  proxy = rest_proxy_new ("https://getpocket.com/", FALSE);
 
83
  call = rest_proxy_new_call (proxy);
 
84
  rest_proxy_call_set_method (call, "POST");
 
85
  rest_proxy_call_set_function (call, "v3/add");
 
86
  rest_proxy_call_add_param (call, "consumer_key", goa_oauth2_based_get_client_id (oauth2));
 
87
  rest_proxy_call_add_param (call, "access_token", access_token);
 
88
  rest_proxy_call_add_param (call, "url", url);
 
89
  if (tweet_id)
 
90
    rest_proxy_call_add_param (call, "tweet_id", tweet_id);
 
91
 
 
92
  g_print ("Adding to Pocket...\n");
 
93
 
 
94
  if (!rest_proxy_call_sync (call, &error)) {
 
95
    g_error ("Cannot add to Pocket: %s", error->message);
 
96
    return 1;
 
97
  }
 
98
 
 
99
  g_print ("Added!\n");
 
100
 
 
101
  return 0;
 
102
}