Merge branch 'master' of git@83.233.175.44:hermes
[hermes] / package / src / org / maemo / hermes / engine / linkedin / api.py
index 0d9eb24..fe0c80f 100644 (file)
@@ -13,10 +13,12 @@ class LinkedInApi():
     GCONF_API_KEY = '/apps/maemo/hermes/linkedin_key'
     GCONF_API_SECRET = '/apps/maemo/hermes/linkedin_secret'
     GCONF_ACCESS_TOKEN = '/apps/maemo/hermes/linkedin_access_token'
+    GCONF_USER = '/apps/maemo/hermes/linkedin_user'
     
     LI_SERVER = "api.linkedin.com"
     LI_API_URL = "https://api.linkedin.com"
     LI_CONN_API_URL = LI_API_URL + "/v1/people/~/connections"
+    LI_PROFILE_API_URL = LI_API_URL + "/v1/people/~"
 
     REQUEST_TOKEN_URL = LI_API_URL + "/uas/oauth/requestToken"
     AUTHORIZE_URL = LI_API_URL + "/uas/oauth/authorize"
@@ -42,8 +44,6 @@ class LinkedInApi():
 
         self.consumer = oauth.OAuthConsumer(api_key, secret_key)
         self.sig_method = oauth.OAuthSignatureMethod_HMAC_SHA1()
-        
-        self._verify_browser_command()
 
 
     # -----------------------------------------------------------------------
@@ -51,7 +51,7 @@ class LinkedInApi():
         need_auth()
         token = self._get_request_token()
         url = self._get_authorize_url(token)
-        verifier = block_for_auth(True)
+        verifier = block_for_auth(url)
         self._verify_verifier(token, verifier)
     
     
@@ -158,6 +158,9 @@ class LinkedInApi():
         try:
             token = oauth.OAuthToken.from_string(response)
         except Exception, e:
+            import traceback
+            traceback.print_exc()
+            print response
             raise Exception("Authorization failure - failed to get request token")
         return token
 
@@ -184,8 +187,8 @@ class LinkedInApi():
         connection.request(oauth_request.http_method, self.ACCESS_TOKEN_URL, headers=oauth_request.to_header()) 
         response = connection.getresponse()
         token_str = response.read()
-        if "ouath_problem" in token_str:
-            raise Exception("Authorization failure - failed to get access token")
+        if 'oauth_problem' in token_str:
+            raise Exception("Authorization failure - failed to get access token (" + token_str + ")")
         self._store_access_token_in_gconf(token_str)
         return oauth.OAuthToken.from_string(token_str)
 
@@ -194,22 +197,21 @@ class LinkedInApi():
     def _verify_verifier(self, request_token, verifier):
         try:
             self.access_token = self._get_access_token(request_token, verifier)
+            xml = self._make_api_request(self.LI_PROFILE_API_URL)
+            dom = parseString(xml)
+            friends = self._parse_dom(dom)
+            self._gc.set_string(LinkedInApi.GCONF_USER, friends[0].get_name())
         except Exception, e:
             import traceback
             traceback.print_exc()
-            raise Exception("LinkedIn authorization failed, try again (" + e + ")")
-
-
-    # -----------------------------------------------------------------------
-    def _verify_browser_command(self):
-        # -- Check the environment is going to work...
-        # FIXME: duplication
-        if (self._gc.get_string('/desktop/gnome/url-handlers/http/command') == 'epiphany %s'):
-            raise Exception('Browser in gconf invalid (see NB#136012). Installation error.')
+            raise Exception("LinkedIn authorization failed, try again")
 
 
     # -----------------------------------------------------------------------
     def _store_access_token_in_gconf(self, token_str):
+        if "oauth_problem" in token_str:
+            raise Exception("Authorization failure - access token reported OAuth problem")
+        
         self._gc.set_string(LinkedInApi.GCONF_ACCESS_TOKEN, token_str)
 
         
@@ -220,9 +222,6 @@ class LinkedInApi():
         token_str = self._gc.get_string(LinkedInApi.GCONF_ACCESS_TOKEN)
         if not token_str:
             return None
-        if "oauth_problem" in token_str:
-            self._store_access_token_in_gconf("")
-            raise Exception("Authorization failure - access token reported OAuth problem")
         return oauth.OAuthToken.from_string(token_str)
 
 
@@ -230,4 +229,5 @@ class LinkedInApi():
     def remove_access_token_from_gconf(self):
         """Remove the oauth.OAuthToken, if any."""
         
-        self._gc.unset(LinkedInAPI.GCONF_ACCESS_TOKEN)
+        self._gc.unset(LinkedInApi.GCONF_ACCESS_TOKEN)
+        self._gc.unset(LinkedInApi.GCONF_USER)