/soc/2015/nakulgulati/main: aa8ce6ceb927: hangouts: api,- refres...

Nakul at rock.pidgin.im Nakul at rock.pidgin.im
Fri Aug 7 05:02:58 EDT 2015


Changeset: aa8ce6ceb927a64f8b187f05ea220483118650ce
Author:	 Nakul Gulati
Date:	 2015-08-07 11:37 +0800
Branch:	 hangouts
URL: https://hg.pidgin.im/soc/2015/nakulgulati/main/rev/aa8ce6ceb927

Description:

hangouts: api,- refresh token

diffstat:

 libpurple/protocols/hangouts/api.c |  76 +++++++++++++++++++++++++++++---------
 libpurple/protocols/hangouts/api.h |  10 ++---
 2 files changed, 62 insertions(+), 24 deletions(-)

diffs (156 lines):

diff --git a/libpurple/protocols/hangouts/api.c b/libpurple/protocols/hangouts/api.c
--- a/libpurple/protocols/hangouts/api.c
+++ b/libpurple/protocols/hangouts/api.c
@@ -138,18 +138,6 @@ hangouts_api_new (PurpleConnection *gc)
 }
 
 void
-hangouts_api_set_access_token (HangoutsApi *api, gchar *access_token)
-{
-  purple_debug_misc ("hangouts-prpl", "new: %s", access_token);
-  HangoutsApiPrivate *priv;
-
-  priv = api->priv;
-  purple_debug_misc ("hangouts-prpl", "old: %s", priv->access_token);
-  priv->access_token = g_strdup (access_token);
-
-}
-
-void
 hangouts_api_parse_header (HangoutsApiCookies *cookies, GList *headers)
 {
   GList *l;
@@ -290,34 +278,32 @@ hangouts_api_get_galx_cookie (HangoutsAp
 
 static void
 hangouts_api_auth_with_code_cb (PurpleHttpConnection *http_conn,
-				PurpleHttpResponse *response, gpointer *data)
+				PurpleHttpResponse *response,
+				gpointer *user_data)
 {
   JsonParser *parser;
   JsonObject *json_ob;
-  PurpleConnection *gc;
   HangoutsApi *api;
   HangoutsApiOAuthData *oauth_data;
   gchar *error, *access_token, *refresh_token;
 
-  api = data;
+  api = user_data;
   oauth_data = api->priv->oauth_data;
   gchar *raw_res = purple_http_response_get_data (response, NULL);
+  purple_debug_misc ("hangouts-prpl", "raw:\n%s", raw_res);
 
   parser = json_parser_new ();
   json_parser_load_from_data (parser, raw_res, -1, NULL);
   json_ob = json_node_get_object (json_parser_get_root (parser));
 
   if (json_object_has_member (json_ob, "error"))
-    /*TODO Throw error*/
     error = json_object_get_string_member (json_ob, "error");
 
   if (json_object_has_member (json_ob, "access_token"))
-    /*TODO store token for api_access*/
     api->priv->access_token = g_strdup (
 	json_object_get_string_member (json_ob, "access_token"));
 
   if (json_object_has_member (json_ob, "refresh_token"))
-    /*TODO store token for refresh request*/
     oauth_data->refresh_token = g_strdup (
 	json_object_get_string_member (json_ob, "refresh_token"));
 
@@ -354,6 +340,60 @@ hangouts_api_auth_with_code (HangoutsApi
 }
 
 static void
+hangouts_api_oauth_refresh_token_cb (PurpleHttpConnection *http_conn,
+				     PurpleHttpResponse *response,
+				     gpointer user_data)
+{
+  JsonParser *parser;
+  JsonObject *json_ob;
+  HangoutsApi *api;
+  gchar *error, *access_token, *refresh_token;
+
+  api = user_data;
+  gchar *raw_res = purple_http_response_get_data (response, NULL);
+  purple_debug_misc ("hangouts-prpl", "raw:\n%s", raw_res);
+
+  parser = json_parser_new ();
+  json_parser_load_from_data (parser, raw_res, -1, NULL);
+  json_ob = json_node_get_object (json_parser_get_root (parser));
+
+  if (json_object_has_member (json_ob, "error"))
+    /*TODO Throw error*/
+    error = json_object_get_string_member (json_ob, "error");
+
+  if (json_object_has_member (json_ob, "access_token"))
+    api->priv->access_token = g_strdup (
+	json_object_get_string_member (json_ob, "access_token"));
+}
+
+void
+hangouts_api_oauth_refresh_token (HangoutsApi *api)
+{
+  PurpleHttpRequest *request;
+  PurpleConnection *gc;
+  HangoutsApiPrivate *priv;
+  gchar *request_data;
+
+  priv = api->priv;
+  gc = priv->gc;
+
+  request_data = g_strdup_printf ("client_id=%s"
+				  "&refresh_token=%s"
+				  "&grant_type=%s"
+  HANGOUTS_API_OAUTH2_CLIENT_ID,
+				  priv->oauth_data->refresh_token,
+				  HANGOUTS_API_OAUTH2_GRANT_TYPE_REFRESH_CODE);
+
+  request = purple_http_request_new (HANGOUTS_API_OAUTH2_TOKEN_URL);
+  purple_http_request_set_method (request, "POST");
+  purple_http_request_header_set (request, "Content-Type",
+				  "application/x-www-form-urlencoded");
+  purple_http_request_set_contents (request, request_data, -1);
+
+  purple_http_request (gc, request, hangouts_api_oauth_refresh_token_cb, api);
+}
+
+static void
 hangouts_api_get_private_token_cb (PurpleHttpConnection *http_conn,
 				   PurpleHttpResponse *response,
 				   gpointer user_data)
diff --git a/libpurple/protocols/hangouts/api.h b/libpurple/protocols/hangouts/api.h
--- a/libpurple/protocols/hangouts/api.h
+++ b/libpurple/protocols/hangouts/api.h
@@ -35,6 +35,7 @@ https://www.googleapis.com/auth/hangouts
 #define HANGOUTS_API_OAUTH2_TOKEN_URL "https://www.googleapis.com/oauth2/v3/token"
 #define HANGOUTS_API_OAUTH2_AUTHORIZATION_CODE_URL "https://accounts.google.com/o/oauth2/auth?\
 response_type=code\
+&access_type=offline\
 &scope=" HANGOUTS_API_OAUTH2_SCOPE \
 "&client_id=" HANGOUTS_API_OAUTH2_CLIENT_ID \
 "&redirect_uri=" HANGOUTS_API_OAUTH2_REDIRECT_URI
@@ -95,12 +96,6 @@ HangoutsApi *
 hangouts_api_new_from_oauth (PurpleConnection *gc, gchar *access_token);
 
 void
-hangouts_api_set_access_token (HangoutsApi *api, gchar *access_token);
-
-void
-hangouts_api_set_protocol_data (HangoutsApi *api);
-
-void
 hangouts_api_parse_header (HangoutsApiCookies *cookies, GList *headers);
 
 void
@@ -110,6 +105,9 @@ void
 hangouts_api_auth_with_code (HangoutsApi *api, gchar *auth_code);
 
 void
+hangouts_api_oauth_refresh_token (HangoutsApi *api);
+
+void
 hangouts_api_fetch_login_cookies (HangoutsApi *api);
 
 void



More information about the Commits mailing list