/soc/2015/nakulgulati/main: 6fc5518db217: hangouts: api integrat...
Nakul at rock.pidgin.im
Nakul at rock.pidgin.im
Thu Aug 6 21:02:32 EDT 2015
Changeset: 6fc5518db217ce99dead4f76aa3779dd9238320f
Author: Nakul Gulati
Date: 2015-08-07 08:59 +0800
Branch: hangouts
URL: https://hg.pidgin.im/soc/2015/nakulgulati/main/rev/6fc5518db217
Description:
hangouts: api integration with oauth, oauth fixes
diffstat:
libpurple/protocols/hangouts/api.c | 127 ++++++++++++++++++++++++++++++------
libpurple/protocols/hangouts/api.h | 31 +++++++++
2 files changed, 137 insertions(+), 21 deletions(-)
diffs (269 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
@@ -21,6 +21,7 @@
#include <string.h>
#include <glib.h>
+#include <json-glib/json-glib.h>
#include "http.h"
@@ -47,11 +48,19 @@ struct _HangoutsApiCookies
};
+struct _HangoutsApiOAuthData
+{
+ gchar *refresh_token;
+ gchar *expires_in;
+};
+
struct _HangoutsApiPrivate
{
PurpleConnection *gc;
PurpleHttpCookieJar *cookie_jar;
+
HangoutsApiCookies *cookies;
+ HangoutsApiOAuthData *oauth_data;
gchar *api_key;
gchar *access_token;
@@ -84,6 +93,7 @@ hangouts_api_dispose (GObject *obj)
purple_http_cookie_jar_unref (priv->cookie_jar);
g_free (priv->cookies);
+ g_free (priv->oauth_data);
g_free (priv->access_token);
g_free (priv->api_key);
g_free (priv->client_id);
@@ -121,13 +131,25 @@ hangouts_api_new (PurpleConnection *gc)
api = g_object_new (HANGOUTS_TYPE_API, NULL);
priv = api->priv;
priv->cookies = g_new0(HangoutsApiCookies, 1);
-
+ priv->oauth_data = g_new0(HangoutsApiOAuthData, 1);
priv->gc = gc;
return api;
}
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;
@@ -194,7 +216,7 @@ hangouts_api_fetch_cookies_cb (PurpleHtt
"Set-Cookie");
hangouts_api_parse_header (priv->cookies, headers);
- hangouts_api_get_private_token (api);
+ //hangouts_api_get_private_token (api);
}
void
@@ -213,11 +235,15 @@ hangouts_api_fetch_login_cookies (Hangou
password = purple_connection_get_password (gc);
url = "https://accounts.google.com/ServiceLoginAuth";
- request_body = g_strdup_printf ("&PersistentCookie=yes"
- "&Email=%s"
- "&Passwd=%s"
- "&GALX=%s",
- username, password, priv->cookies->GALX);
+ /*request_body = g_strdup_printf ("&PersistentCookie=yes"
+ "&Email=%s"
+ "&Passwd=%s"
+ "&GALX=%s",
+ username, password, priv->cookies->GALX);*/
+ request_body =
+ g_strdup_printf (
+ "GALX=%s&continue=https://talkgadget.google.com/talkgadget/gauth?verify=true&skipvpage=true&_utf8=â&bgresponse=js_disabled&pstMsg=0&dnConn=&checkConnection=&checkedDomains=youtube&Email=nakul.gulati@st.niituniversity.in&Passwd=glukan4294&signIn=PÅihlásit se&PersistentCookie=yes&rmShown=1",
+ priv->cookies->GALX);
request = purple_http_request_new (url);
purple_http_request_set_method (request, "POST");
@@ -258,7 +284,9 @@ hangouts_api_get_galx_cookie (HangoutsAp
priv = api->priv;
gc = priv->gc;
- url = "https://accounts.google.com/ServiceLogin?service=ah&passive=true";
+ /*url = "https://accounts.google.com/ServiceLogin?service=ah&passive=true";*/
+ url =
+ "https://accounts.google.com/ServiceLogin?passive=true&skipvpage=true&continue=https://talkgadget.google.com/talkgadget/gauth?verify%3Dtrue&authuser=0";
request = purple_http_request_new (url);
cookieJar = purple_http_cookie_jar_new ();
@@ -268,6 +296,71 @@ hangouts_api_get_galx_cookie (HangoutsAp
}
static void
+hangouts_api_auth_with_code_cb (PurpleHttpConnection *http_conn,
+ PurpleHttpResponse *response, gpointer *data)
+{
+ JsonParser *parser;
+ JsonObject *json_ob;
+ PurpleConnection *gc;
+ HangoutsApi *api;
+ HangoutsApiOAuthData *oauth_data;
+ gchar *error, *access_token, *refresh_token;
+
+ api = data;
+ oauth_data = api->priv->oauth_data;
+ gchar *raw_res = purple_http_response_get_data (response, NULL);
+
+ 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"));
+
+ hangouts_api_get_private_token (api);
+}
+
+void
+hangouts_api_auth_with_code (HangoutsApi *api, gchar *auth_code)
+{
+ PurpleHttpRequest *request;
+ PurpleConnection *gc;
+ HangoutsApiPrivate *priv;
+ gchar *request_data;
+
+ priv = api->priv;
+ gc = priv->gc;
+ request = purple_http_request_new (HANGOUTS_API_OAUTH2_TOKEN_URL);
+
+ request_data = g_strdup_printf (
+ "client_id=%s&"
+ "code=%s&"
+ "grant_type=%s&"
+ "redirect_uri=%s",
+ HANGOUTS_API_OAUTH2_CLIENT_ID, auth_code,
+ HANGOUTS_API_OAUTH2_GRANT_TYPE_AUTHORIZATION_CODE,
+ HANGOUTS_API_OAUTH2_REDIRECT_URI);
+
+ 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_auth_with_code_cb, api);
+}
+
+static void
hangouts_api_get_private_token_cb (PurpleHttpConnection *http_conn,
PurpleHttpResponse *response,
gpointer user_data)
@@ -291,22 +384,14 @@ hangouts_api_get_private_token (Hangouts
priv = api->priv;
gc = priv->gc;
cookies = priv->cookies;
- request = purple_http_request_new (HANGOUTS_API_PVT_TOKEN_URL);
+ request = purple_http_request_new (
+ "https://talkgadget.google.com/talkgadget/_/extension-start");
purple_http_request_set_method (request, "GET");
+ purple_http_request_header_add (
+ request, "Authorization",
+ g_strdup_printf ("Bearer %s", priv->access_token));
purple_http_request_set_max_len (request, -1);
- purple_http_request_header_add (
- request, "Cookie", g_strdup_printf ("APISID %s", cookies->APISID));
- purple_http_request_header_add (request, "Cookie",
- g_strdup_printf ("HSID %s", cookies->HSID));
- purple_http_request_header_add (request, "Cookie",
- g_strdup_printf ("NID %s", cookies->NID));
- purple_http_request_header_add (
- request, "Cookie", g_strdup_printf ("SAPISID %s", cookies->SAPISID));
- purple_http_request_header_add (request, "Cookie",
- g_strdup_printf ("SID %s", cookies->SID));
- purple_http_request_header_add (request, "Cookie",
- g_strdup_printf ("SSID %s", cookies->SSID));
purple_http_request (gc, request, hangouts_api_get_private_token_cb, api);
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
@@ -24,6 +24,24 @@
#include "connection.h"
+#define HANGOUTS_API_OAUTH2_CLIENT_ID "1021803392181-71juemdl03adpl2n0tso7nl7hq83g2qg.apps.googleusercontent.com"
+#define HANGOUTS_API_OAUTH2_CLIENT_SECRET "bjdsfbsdjf"
+#define HANGOUTS_API_OAUTH2_REDIRECT_URI "urn:ietf:wg:oauth:2.0:oob"
+#define HANGOUTS_API_OAUTH2_SCOPE "https://www.googleapis.com/auth/chat%20\
+https://www.googleapis.com/auth/client_channel%20\
+https://www.googleapis.com/auth/googlevoice%20\
+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\
+&scope=" HANGOUTS_API_OAUTH2_SCOPE \
+"&client_id=" HANGOUTS_API_OAUTH2_CLIENT_ID \
+"&redirect_uri=" HANGOUTS_API_OAUTH2_REDIRECT_URI
+
+#define HANGOUTS_API_OAUTH2_GRANT_TYPE_AUTHORIZATION_CODE "authorization_code"
+#define HANGOUTS_API_OAUTH2_GRANT_TYPE_REFRESH_CODE "refresh_token"
+
#define HANGOUTS_API_GALX_COOKIE_URL "https://accounts.google.com/ServiceLogin?service=ah&passive=true"
#define HANGOUTS_API_LOGIN_URL "https://accounts.google.com/ServiceLoginAuth"
@@ -54,6 +72,7 @@ typedef struct _HangoutsApiClass Hangout
typedef struct _HangoutsApiPrivate HangoutsApiPrivate;
typedef struct _HangoutsApiHttpInfo HangoutsApiHttpInfo;
typedef struct _HangoutsApiCookies HangoutsApiCookies;
+typedef struct _HangoutsApiOAuthData HangoutsApiOAuthData;
struct _HangoutsApi
{
@@ -72,6 +91,15 @@ hangouts_api_get_type (void);
HangoutsApi *
hangouts_api_new (PurpleConnection *gc);
+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);
@@ -79,6 +107,9 @@ void
hangouts_api_get_galx_cookie (HangoutsApi *api);
void
+hangouts_api_auth_with_code (HangoutsApi *api, gchar *auth_code);
+
+void
hangouts_api_fetch_login_cookies (HangoutsApi *api);
void
More information about the Commits
mailing list