def test_request_access_token_failure(self): error_message = {'error_reason': 'authorization not given'} response = requests.Response() response.status_code = requests.codes.conflict response._content = json.dumps(error_message) self.replacer.replace('requests.post', lambda *args, **kwargs: response) call = lambda: token_utils.request_access_token( 'myid', 'mysecret', 'theauthcode', 'http://oauth.org/2/authorize') self.assertRaises(token_utils.TokenRequestError, call)
def test_request_access_token_failure(self): error_message = { 'error_reason': 'authorization not given' } response = requests.Response() response.status_code = requests.codes.conflict response._content = json.dumps(error_message) self.replacer.replace('requests.post', lambda *args, **kwargs: response) call = lambda: token_utils.request_access_token('myid', 'mysecret','theauthcode', 'http://oauth.org/2/authorize') self.assertRaises(token_utils.TokenRequestError, call)
def get_access_token_from_code(self, code): """ After receiving a code from the end user, this method will acquire an access token from the server which can be used for subsequent requests. :param code: The code which the user received after authenticating with the server and authorizing the client. :return: Tuple containing (access_token, refresh_token, expire_time) """ url_parts = ('https', self.server, '/goauth/token', None, None) result = token_utils.request_access_token( self.api_key, self.api_secret, code, urlparse.urlunsplit(url_parts)) return (result.access_token, result.refresh_token, time.mktime(datetime.utcnow().timetuple()) + result.expires_in)
def get_access_token_from_code(self, code): """ After receiving a code from the end user, this method will acquire an access token from the server which can be used for subsequent requests. :param code: The code which the user received after authenticating with the server and authorizing the client. :return: Tuple containing (access_token, refresh_token, expire_time) """ url_parts = ('https', self.server, '/goauth/token', None, None) result = token_utils.request_access_token(self.client, self.client_secret, code, urlparse.urlunsplit(url_parts)) return ( result.access_token, result.refresh_token, time.mktime(datetime.utcnow().timetuple()) + result.expires_in )
def goauth_get_access_token_from_code(self, code, redirect_uri=None): """ After receiving a code from the end user, this method will acquire an access token from the server which can be used for subsequent requests. :param code: The code which the user received after authenticating with the server and authorizing the client. :param redirect_uri: The redirect URI which will be used for token exchange. Per OAuth2 spec, this must be passed here. :return: Tuple containing (access_token, refresh_token, expire_time) """ url_parts = ('https', self.server, '/goauth/token', None, None) result = token_utils.request_access_token( self.client, self.client_secret, code, urlparse.urlunsplit(url_parts), redirect_uri=redirect_uri) return (result.access_token, result.refresh_token, time.mktime(datetime.utcnow().timetuple()) + result.expires_in)
def test_request_access_token(self): response = requests.Response() response.status_code = requests.codes.ok access_token = { "access_token": "faohwefadfawaw", "refresh_token": "fhajhkjbhrafw", "expires_in": 123456789, "token_type": "Bearer" } response._content = json.dumps(access_token) self.replacer.replace('requests.post', lambda *args, **kwargs: response) token_map = token_utils.request_access_token('myid', 'mysecret','theauthcode', 'http://oauth.org/2/authorize') self.assertEqual('faohwefadfawaw', token_map['access_token']) self.assertEqual('faohwefadfawaw', token_map.access_token) self.assertEqual('fhajhkjbhrafw', token_map['refresh_token']) self.assertEqual('fhajhkjbhrafw', token_map.refresh_token) self.assertEqual(123456789, token_map['expires_in']) self.assertEqual(123456789, token_map.expires_in) self.assertEqual('Bearer', token_map['token_type']) self.assertEqual('Bearer', token_map.token_type)
def test_request_access_token(self): response = requests.Response() response.status_code = requests.codes.created access_token = { "access_token": "faohwefadfawaw", "refresh_token": "fhajhkjbhrafw", "expires_in": 123456789, "token_type": "Bearer" } response._content = json.dumps(access_token) self.replacer.replace('requests.post', lambda *args, **kwargs: response) token_map = token_utils.request_access_token( 'myid', 'mysecret', 'theauthcode', 'http://oauth.org/2/authorize') self.assertEqual('faohwefadfawaw', token_map['access_token']) self.assertEqual('faohwefadfawaw', token_map.access_token) self.assertEqual('fhajhkjbhrafw', token_map['refresh_token']) self.assertEqual('fhajhkjbhrafw', token_map.refresh_token) self.assertEqual(123456789, token_map['expires_in']) self.assertEqual(123456789, token_map.expires_in) self.assertEqual('Bearer', token_map['token_type']) self.assertEqual('Bearer', token_map.token_type)