def fetch_access_token(self, redirect_uri, authorization_code): """Fetch the access token for a merchant Takes the authorization code obtained from a merchant redirect and the redirect_uri used in that same redirect and fetches the corresponding access token. The access token is returned and also set on the client so the client can then be used to make api calls on behalf of the merchant. :param redirect_uri: The redirect_uri used in the request which obtained the authorization code, must match exactly. :param authorization_code: The authorization code obtained in the previous part of the process. """ params = { "client_id": self._app_id, "code": authorization_code, "redirect_uri": redirect_uri, "grant_type": "authorization_code" } query = to_query(params) url = "/oauth/access_token?{0}".format(query) # have to use _request so we don't add api_base to the url auth_details = (self._app_id, self._app_secret) result = self._request("post", url, auth=auth_details) self._access_token = result["access_token"] self._merchant_id = result["scope"].split(":")[1] return self._access_token
def test_fetch_client_access_token_basic_authorization(self): expected_data = { "client_id":mock_account_details["app_id"], "code":self.mock_auth_code, "redirect_uri":"http://someurl", "grant_type":"authorization_code" } query = utils.to_query(expected_data) expected_auth = ( mock_account_details["app_id"], mock_account_details["app_secret"]) with patch.object(self.client, '_request') as mock_request: mock_request.return_value = self.access_token_response self.client.fetch_access_token(expected_data["redirect_uri"], self.mock_auth_code) mock_request.assert_called_with("post", "/oauth/" "access_token?{0}".format(query), auth=expected_auth)
def new_merchant_url(self, redirect_uri, state=None, merchant=None): """Get a URL for managing a new merchant This method creates a URL which partners should redirect merchants to in order to obtain permission to manage their GoCardless payments. :param redirect_uri: The URI where the merchant will be sent after authorizing. :param state: An optional string which will be present in the request to the redirect URI, useful for tracking the user. :param merchant: A dictionary which will be used to prepopulate the merchant sign up page, can contain any of the keys: - "name" - "phone_number" - "description" - "merchant_type" (either 'business', 'charity' or 'individual') - "company_name" - "company_registration" - "user" which can be a dictionary containing the keys: - "first_name" - "last_name" - "email" """ params = { "client_id": self._app_id, "redirect_uri": redirect_uri, "scope": "manage_merchant", "response_type": "code", } if state: params["state"] = state if merchant: params["merchant"] = merchant return "{0}/oauth/authorize?{1}".format(self.get_base_url(), to_query(params))