[gnome-online-accounts/wip/pocket: 3/4] examples: Add a Pocket example



commit f7589df57f0996f5ab67b43cd1ccfcdc54e1a218
Author: Bastien Nocera <hadess hadess net>
Date:   Sat Dec 14 03:30:51 2013 +0100

    examples: Add a Pocket example
    
    This example adds the passed URL to the first Pocket account
    listed setup in GOA. Optionally, you can pass a tweet ID which
    would be used to show where the user got the URL from.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=704564

 src/examples/Makefile.am  |    6 ++-
 src/examples/add-pocket.c |  104 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 109 insertions(+), 1 deletions(-)
---
diff --git a/src/examples/Makefile.am b/src/examples/Makefile.am
index 1b2b2d9..7ee5a8a 100644
--- a/src/examples/Makefile.am
+++ b/src/examples/Makefile.am
@@ -15,7 +15,7 @@ INCLUDES =                                                    \
        $(WARN_CFLAGS)                                          \
        $(NULL)
 
-noinst_PROGRAMS = list-providers list-accounts tweet
+noinst_PROGRAMS = list-providers list-accounts tweet add-pocket
 
 list_providers_SOURCES = list-providers.c
 list_providers_CFLAGS = $(GLIB_CFLAGS) $(GTK_CFLAGS)
@@ -29,6 +29,10 @@ tweet_SOURCES = tweet.c
 tweet_CFLAGS = $(GLIB_CFLAGS) $(REST_CFLAGS)
 tweet_LDADD = $(GLIB_LIBS) $(REST_LIBS) ../goa/libgoa-1.0.la
 
+add_pocket_SOURCES = add-pocket.c
+add_pocket_CFLAGS = $(GLIB_CFLAGS) $(REST_CFLAGS)
+add_pocket_LDADD = $(GLIB_LIBS) $(REST_LIBS) ../goa/libgoa-1.0.la
+
 clean-local :
        rm -f *~
 
diff --git a/src/examples/add-pocket.c b/src/examples/add-pocket.c
new file mode 100644
index 0000000..bcc8c1d
--- /dev/null
+++ b/src/examples/add-pocket.c
@@ -0,0 +1,104 @@
+/*
+ * Copyright (C) 2013 Bastien Nocera <hadess hadess net>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Author: Bastien Nocera <hadess hadess net>
+ */
+
+#include <config.h>
+
+#define GOA_API_IS_SUBJECT_TO_CHANGE
+#include <goa/goa.h>
+
+#include <rest/rest-proxy.h>
+#include <rest/rest-proxy-call.h>
+
+int
+main (int argc, char **argv)
+{
+  GError *error = NULL;
+  GoaClient *client;
+  GList *accounts, *l;
+  GoaAccount *account = NULL;
+  GoaOAuth2Based *oauth2 = NULL;
+  char *access_token = NULL;
+  RestProxy *proxy;
+  RestProxyCall *call;
+  const char *url;
+  const char *tweet_id = NULL;
+
+  if (argc != 2 && argc != 3) {
+    g_print ("Usage: %s URL [TWEET ID]\n", argv[0]);
+    return 1;
+  }
+  url = argv[1];
+  if (argv[2] != NULL)
+    tweet_id = argv[2];
+
+  client = goa_client_new_sync (NULL, &error);
+  if (!client) {
+    g_error ("Could not create GoaClient: %s", error->message);
+    return 1;
+  }
+
+  accounts = goa_client_get_accounts (client);
+
+  /* Find a Pocket account */
+  for (l = accounts; l != NULL; l = l->next) {
+    GoaObject *object = GOA_OBJECT (l->data);
+
+    account = goa_object_peek_account (object);
+    if (g_strcmp0 (goa_account_get_provider_type (account), "pocket") == 0) {
+      g_object_ref (account);
+      oauth2 = goa_object_get_oauth2_based (object);
+      break;
+    }
+  }
+
+  g_list_free_full (accounts, (GDestroyNotify) g_object_unref);
+
+  g_assert (account);
+  g_assert (oauth2);
+
+  if (!goa_oauth2_based_call_get_access_token_sync (oauth2, &access_token, NULL, NULL, &error)) {
+    g_error ("Could not get access token: %s", error->message);
+    return 1;
+  }
+
+  g_print ("Got access tokens\n");
+
+  proxy = rest_proxy_new ("https://getpocket.com/";, FALSE);
+  call = rest_proxy_new_call (proxy);
+  rest_proxy_call_set_method (call, "POST");
+  rest_proxy_call_set_function (call, "v3/add");
+  rest_proxy_call_add_param (call, "consumer_key", goa_oauth2_based_get_client_id (oauth2));
+  rest_proxy_call_add_param (call, "access_token", access_token);
+  rest_proxy_call_add_param (call, "url", url);
+  if (tweet_id)
+    rest_proxy_call_add_param (call, "tweet_id", tweet_id);
+
+  g_print ("Adding to Pocket...\n");
+
+  if (!rest_proxy_call_sync (call, &error)) {
+    g_error ("Cannot add to Pocket: %s", error->message);
+    return 1;
+  }
+
+  g_print ("Added!\n");
+
+  return 0;
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]