def get_user_info(self, oauth_token, oauth_verifier): response = self.__get_access_token( oauth_token=oauth_token, oauth_verifier=oauth_verifier ) if response.status_code is not 200: raise TwitterOauthError( endpoint=settings.TWITTER_API_ACCESS_TOKEN_URL, status_code=response.status_code, message=response.text ) access_token = self.__parse_api_response( response=response ) cognito_user_id = self.__generate_user_id(access_token['user_id']) twitter = OAuth1Session( self.consumer_key, self.consumer_secret, access_token['oauth_token'], access_token['oauth_token_secret'], ) response = twitter.get( settings.TWITTER_API_VERIFY_CREDENTIALS_URL + '?include_email=true' ) if response.status_code is not 200: raise TwitterOauthError( endpoint=settings.TWITTER_API_VERIFY_CREDENTIALS_URL, status_code=response.status_code, message=response.text ) user_info = json.loads(response.text) return { 'user_id': cognito_user_id, 'email': self.__get_email(user_info, cognito_user_id) }
def test_main_ng_with_twitterexception(self): with patch('login_twitter_index.TwitterUtil') as twitter_mock: twitter_mock.return_value.get_user_info.side_effect = TwitterOauthError( endpoint='http://example.com', status_code=500, message='error') params = { 'body': { 'oauth_token': 'fake_oauth_token', 'oauth_verifier': 'fake_oauth_verifier' } } params['body'] = json.dumps(params['body']) response = LoginTwitterIndex(params, {}).main() self.assertEqual(response['statusCode'], 500) self.assertEqual(json.loads(response['body']), {'message': 'Internal server error'})
def generate_auth_url(self, callback_url): twitter = OAuth1Session( self.consumer_key, self.consumer_secret ) response = twitter.post( settings.TWITTER_API_REQUEST_TOKEN_URL, params={'oauth_callback': callback_url} ) if response.status_code is not 200: raise TwitterOauthError( endpoint=settings.TWITTER_API_REQUEST_TOKEN_URL, status_code=response.status_code, message=response.text ) response_body = self.__parse_api_response( response=response ) return '%s?oauth_token=%s' \ % (settings.TWITTER_API_AUTHENTICATE_URL, response_body['oauth_token'])