Search code examples
iosswiftoauthoauth-2.0coinbase-api

Coinbase API invalid request (oauth token)


I am trying to use the Coinbase API for oauth, but keep getting an invalid request response "The request is missing a required parameter, includes an unsupported parameter value, or is otherwise malformed."

Following their guide https://developers.coinbase.com/docs/wallet/coinbase-connect/integrating, I'm able to:

  1. Successfully redirect users to request Coinbase access

GET https://www.coinbase.com/oauth/authorize?response_type=code&client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_REDIRECT_URL&state=SECURE_RANDOM&scope=wallet:accounts:read

  1. Successfully have Coinbase redirects back to your site

GET https://example.com/oauth/callback?code=4c666b5c0c0d9d3140f2e0776cbe245f3143011d82b7a2c2a590cc7e20b79ae8&state=134ef5504a94 app://oauth-coins?code=4c666b5c0c0d9d3140f2e0776cbe245f3143011d82b7a2c2a590cc7e20b79ae8

  1. Unsuccessfully exchange code for an access token POST https://api.coinbase.com/oauth/token

    let url = URL(string: "https://api.coinbase.com/oauth/token")! //
    var request = URLRequest(url: url)
    request.setValue("authorization_code", forHTTPHeaderField: "grant_type")
    request.setValue("\(self.code)", forHTTPHeaderField: "code")
    request.setValue("clientid1234", forHTTPHeaderField: "client_id")
    request.setValue("clientsecret1234", forHTTPHeaderField: "client_secret")
    request.setValue("app://oauth-coins", forHTTPHeaderField: "redirect_uri")
    

Because I get an error return in the json:

{
    error = "invalid_request";
    "error_description" = "The request is missing a required parameter, includes an unsupported parameter value, or is otherwise malformed.";
}

UPDATE (solved):

I needed to not setValue for all of the parameters, but to post them as the request.httpBody


Solution

  • I needed to not setValue for all of the parameters, but to post them as the request.httpBody