def get_access_token(self, request, redirect=None): code = request.GET.get('code') request_url = self.code_base + "client_id={0}&redirect_uri={1}&client_secret={2}&code={3}".format( self.consumer_key, redirect, self.consumer_secret, code ) rcontent = urllib2.urlopen(request_url).read() params = rcontent.split('&') access_token = params[0].split('=')[1] # "expires" parameter is seconds from now. expires_in = timedelta(seconds=int(params[1].split('=')[1])) expires = localize_datetime(datetime.now() + expires_in) # Debug token to get user id, other metadata. app_token = self.consumer_key + "|" + self.consumer_secret request_url = self.dbg_base + "input_token={0}&access_token={1}".format( access_token, app_token ) response = urllib2.urlopen(request_url) # GET rdata = json.load(response)['data'] # Parse JSON response. user_id = rdata['user_id'] platform = SocialPlatform.objects.get(name='Facebook') ptoken = OAuthAccessToken( oauth_access_token=access_token, platform=platform, user_id=user_id, expires=expires ) ptoken.save() self.cleanup() return ptoken.id
def get_access_url(self, callback): """ Generates an access URL to which the user should be redirected in order to authorize Dolon. Parameters ---------- callback : str Callback URL to which the user should be redirected after authorizing Dolon. That view will receive a verifier, which is then used to obtain an access token. Returns ------- redirect_url : str The URL to which the user should be redirected in order to authorize Dolon. ptoken.id : int ID of a :class:`.OAuthAccessToken` containing the original request token. """ redirect_url = self.auth.get_authorization_url() logger.debug('Redirect URL: {0}'.format(redirect_url)) # We must create an OAuthAccessToken here because we need the request # tokens used when the authorization URL was generated. platform = SocialPlatform.objects.get(name='Twitter') ptoken = OAuthAccessToken( oauth_token = self.auth.request_token.key, oauth_token_secret = self.auth.request_token.secret, platform=platform, ) ptoken.save() return redirect_url