[PATCH 01/18] flickr: Add helper function to get a frob
- From: "Juan A. Suarez Romero" <jasuarez igalia com>
- To: grilo-list gnome org
- Subject: [PATCH 01/18] flickr: Add helper function to get a frob
- Date: Wed, 7 Jul 2010 18:19:56 +0200
Flickr requires that users grants permissions to access to Flickr account. So
client applications should shown a message to grant this access in order to
plugin to access Flickr's content.
This function helps client developers in a first step: getting a valid frob.
---
src/flickr/Makefile.am | 19 +++++++-
src/flickr/grl-flickr-auth.c | 89 ++++++++++++++++++++++++++++++++++++++++++
src/flickr/grl-flickr-auth.h | 32 +++++++++++++++
3 files changed, 137 insertions(+), 3 deletions(-)
create mode 100644 src/flickr/grl-flickr-auth.c
create mode 100644 src/flickr/grl-flickr-auth.h
diff --git a/src/flickr/Makefile.am b/src/flickr/Makefile.am
index 41e19e8..a0a099b 100644
--- a/src/flickr/Makefile.am
+++ b/src/flickr/Makefile.am
@@ -5,7 +5,9 @@
#
# Copyright (C) 2010 Igalia S.L. All rights reserved.
-lib_LTLIBRARIES = libgrlflickr.la
+libplugin_LTLIBRARIES = libgrlflickr.la
+
+libauth_LTLIBRARIES = libgrlflickrauth.la
libgrlflickr_la_CFLAGS = \
$(DEPS_CFLAGS) \
@@ -23,9 +25,20 @@ libgrlflickr_la_SOURCES = \
gflickr.c \
gflickr.h
-libdir=$(GRL_PLUGINS_DIR)
+libgrlflickrauth_la_CFLAGS = \
+ $(libgrlflickr_la_CFLAGS)
+
+libgrlflickrauth_la_LIBADD = \
+ $(libgrlflickr_la_LIBADD)
+
+libgrlflickrauth_la_SOURCES = \
+ grl-flickr-auth.c \
+ grl-flickr-auth.h
+
+libplugindir=$(GRL_PLUGINS_DIR)
+libauthdir=$(libdir)
-flickrlibxmldir = $(libdir)
+flickrlibxmldir = $(libplugindir)
flickrlibxml_DATA = libgrlflickr.xml
EXTRA_DIR = $(flickrlibdata_DATA)
diff --git a/src/flickr/grl-flickr-auth.c b/src/flickr/grl-flickr-auth.c
new file mode 100644
index 0000000..fee3f83
--- /dev/null
+++ b/src/flickr/grl-flickr-auth.c
@@ -0,0 +1,89 @@
+#include "grl-flickr-auth.h"
+
+#include <gio/gio.h>
+#include <libxml/parser.h>
+#include <libxml/xpath.h>
+
+#define FLICKR_ENTRYPOINT "http://api.flickr.com/services/rest/?"
+
+gchar *
+grl_flickr_get_frob (const gchar *api_key,
+ const gchar *secret)
+{
+ gchar *to_sign;
+ gchar *api_sig;
+ gchar *url;
+ GVfs *vfs;
+ GFile *uri;
+ gchar *contents;
+ xmlDocPtr xmldoc = NULL;
+ xmlXPathContextPtr xpath_ctx = NULL;
+ xmlXPathObjectPtr xpath_res = NULL;
+ GError *error = NULL;
+ gchar *frob = NULL;
+
+ /* Get api sig */
+ to_sign = g_strdup_printf ("%s"
+ "api_key%s"
+ "method" "flickr.auth.getFrob",
+ secret,
+ api_key);
+ api_sig = g_compute_checksum_for_string (G_CHECKSUM_MD5, to_sign, -1);
+ g_free (to_sign);
+
+ /* Build url */
+ url = g_strdup_printf (FLICKR_ENTRYPOINT
+ "method=flickr.auth.getFrob&"
+ "api_key=%s&"
+ "api_sig=%s",
+ api_key,
+ api_sig);
+ g_free (api_sig);
+
+ /* Load content */
+ vfs = g_vfs_get_default ();
+ uri = g_vfs_get_file_for_uri (vfs, url);
+ g_free (url);
+ if (!g_file_load_contents (uri, NULL, &contents, NULL, NULL, &error)) {
+ g_warning ("Unable to get Flickr's frob: %s", error->message);
+ return NULL;
+ }
+
+ /* Get frob */
+ xmldoc = xmlReadMemory (contents, xmlStrlen ((xmlChar *)contents), NULL, NULL,
+ XML_PARSE_RECOVER | XML_PARSE_NOBLANKS);
+ g_free (contents);
+
+ if (xmldoc) {
+ xpath_ctx = xmlXPathNewContext (xmldoc);
+ if (xpath_ctx) {
+ xpath_res = xmlXPathEvalExpression ((xmlChar *) "/rsp/frob", xpath_ctx);
+ if (xpath_res && xpath_res->nodesetval->nodeTab) {
+ frob = (gchar *) xmlNodeListGetString (xmldoc,
+ xpath_res->nodesetval->nodeTab[0]->xmlChildrenNode,
+ 1);
+ } else {
+ g_warning ("Flick's frob not found");
+ }
+ } else {
+ g_warning ("Unable to create Flickr's XPath");
+ }
+ } else {
+ g_warning ("Unable to parse Flickr XML reply");
+ }
+
+ /* Free data */
+ if (xmldoc) {
+ xmlFreeDoc (xmldoc);
+ }
+
+ if (xpath_ctx) {
+ xmlXPathFreeContext (xpath_ctx);
+ }
+
+ if (xpath_res) {
+ xmlXPathFreeObject (xpath_res);
+ }
+
+ return frob;
+}
diff --git a/src/flickr/grl-flickr-auth.h b/src/flickr/grl-flickr-auth.h
new file mode 100644
index 0000000..8ec238b
--- /dev/null
+++ b/src/flickr/grl-flickr-auth.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2010 Igalia S.L.
+ *
+ * Contact: Iago Toral Quiroga <itoral igalia com>
+ *
+ * Authors: Juan A. Suarez Romero <jasuarez igalia com>
+ *
+ * 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; version 2.1 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., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ */
+
+#ifndef _GRL_FLICKR_AUTH_SOURCE_H_
+#define _GRL_FLICKR_AUTH_SOURCE_H_
+
+#include <glib.h>
+
+gchar * grl_flickr_get_frob (const gchar *api_key,
+ const gchar *secret);
+#endif /* _GRL_FLICKR_AUTH_SOURCE_H_ */
--
1.7.0.4
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]