def retrieve_facebook_access_token(oauth_map): # Start Facebook access token process params = { "client_id": App.facebook_app_id, "client_secret": App.facebook_app_secret, "redirect_uri": get_facebook_token_callback_url(oauth_map), "code": oauth_map.facebook_authorization_code, } response = get_response(FB_URL_ACCESS_TOKEN, params) response_params = get_parsed_params(response) if not response_params or not response_params.get("access_token"): raise OAuthError("Cannot get access_token from Facebook's " + "/oauth/access_token response") # Associate our access token and Google/Facebook's oauth_map.facebook_access_token = response_params["access_token"][0] expires_seconds = 0 try: expires_seconds = int(response_params["expires"][0]) except (ValueError, KeyError): pass if expires_seconds: oauth_map.expires = (datetime.datetime.now() + datetime.timedelta(seconds=expires_seconds)) return oauth_map
def access_user_id_and_email(self, oauth_map): token = OAuthToken(oauth_map.google_access_token, oauth_map.google_access_token_secret) oauth_request = OAuthRequest.from_consumer_and_token( GoogleOAuthClient.Consumer, token=token, http_url="%sapi/auth/current_google_oauth_user_id_and_email" % request.host_url ) oauth_request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), GoogleOAuthClient.Consumer, token) response = get_response(oauth_request.to_url()) try: obj = json.loads(response) if len(obj) == 2: # (user_id, email) return tuple(obj) except ValueError: logging.error("Error decoding json from " "current_google_oauth_user_id_and_email; " "json was %r", response) pass return (None, None)
def get_facebook_app_access_token(): """Gets the Facebook App Access token There is one app access token for the entire Khan Academy Facebook app. It is the same for all users and requests. It only changes when the app secret changes (very rarely; done through the Facebook developer website), so we cache it using setting_model.Setting, which stores per-application key-value pairs for app-wide settings. In case the Facebook app secret is changed (on the Facebook developer site), the request to Facebook is performed for each deployed GAE version. More about the app access token here: https://developers.facebook.com/docs/opengraph/using-app-tokens/ """ params = { "client_id": App.facebook_app_id, "client_secret": App.facebook_app_secret, "grant_type": "client_credentials" } response = get_response(FB_URL_ACCESS_TOKEN, params) response_params = get_parsed_params(response) if response_params: response_array = response_params.get("access_token") if response_array: token = response_array[0] return token raise OAuthError("Cannot get access_token from Facebook's " + "/oauth/access_token response")
def retrieve_facebook_access_token(oauth_map): # Start Facebook access token process params = { "client_id": App.facebook_app_id, "client_secret": App.facebook_app_secret, "redirect_uri": get_facebook_token_callback_url(oauth_map), "code": oauth_map.facebook_authorization_code, } response = get_response(FB_URL_ACCESS_TOKEN, params) response_params = get_parsed_params(response) if not response_params or not response_params.get("access_token"): raise OAuthError("Cannot get access_token from Facebook's " + "/oauth/access_token response") # Associate our access token and Google/Facebook's oauth_map.facebook_access_token = response_params["access_token"][0] expires_seconds = 0 try: expires_seconds = int(response_params["expires"][0]) except (ValueError, KeyError): pass if expires_seconds: oauth_map.expires = datetime.datetime.now() + datetime.timedelta(seconds=expires_seconds) return oauth_map
def get_facebook_app_access_token(): """Gets the Facebook App Access token There is one app access token for the entire Khan Academy Facebook app. It is the same for all users and requests. It only changes when the app secret changes (very rarely; done through the Facebook developer website), so we cache it using setting_model.Setting, which stores per-application key-value pairs for app-wide settings. In case the Facebook app secret is changed (on the Facebook developer site), the request to Facebook is performed for each deployed GAE version. More about the app access token here: https://developers.facebook.com/docs/opengraph/using-app-tokens/ """ params = { "client_id": App.facebook_app_id, "client_secret": App.facebook_app_secret, "grant_type": "client_credentials", } response = get_response(FB_URL_ACCESS_TOKEN, params) response_params = get_parsed_params(response) if response_params: response_array = response_params.get("access_token") if response_array: token = response_array[0] return token raise OAuthError("Cannot get access_token from Facebook's " + "/oauth/access_token response")
def fetch_request_token(self, oauth_map): oauth_request = OAuthRequest.from_consumer_and_token( GoogleOAuthClient.Consumer, http_url = "%s/_ah/OAuthGetRequestToken" % App.realm, callback = "%sapi/auth/google_token_callback?oauth_map_id=%s" % (request.host_url, oauth_map.key().id()) ) oauth_request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), GoogleOAuthClient.Consumer, None) response = get_response(oauth_request.to_url()) return OAuthToken.from_string(response)
def retrieve_facebook_access_token(oauth_map): # Start Facebook access token process params = { "client_id": App.facebook_app_id, "client_secret": App.facebook_app_secret, "redirect_uri": get_facebook_token_callback_url(oauth_map), "code": oauth_map.facebook_authorization_code, } try: response = get_response(FB_URL_ACCESS_TOKEN, params) except Exception, e: raise OAuthError(e.message)
def fetch_access_token(self, oauth_map): token = OAuthToken(oauth_map.google_request_token, oauth_map.google_request_token_secret) oauth_request = OAuthRequest.from_consumer_and_token( GoogleOAuthClient.Consumer, token = token, verifier = oauth_map.google_verification_code, http_url = "%s/_ah/OAuthGetAccessToken" % App.realm ) oauth_request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), GoogleOAuthClient.Consumer, token) response = get_response(oauth_request.to_url()) return OAuthToken.from_string(response)