question

jm1383 avatar image
0 Likes"
jm1383 asked ·

Seem to have a valid access token, but still getting a 401 on a simple request

Hello! I can't seem to figure out why I'm not getting authenticated for search results. This is my Ruby script.

site_path = 'https://api.sandbox.ebay.com/identity/v1/oauth2/token'
auth_body = "grant_type=client_credentials&scope=#{URI.encode_www_form_component('https://api.ebay.com/oauth/api_scope')}"
auth_client_secret = "#{ENV['EBAY_CLIENT_ID']}:#{ENV['EBAY_CLIENT_SECRET']}"
auth_request = Typhoeus::Request.new(
  site_path,
  method: :post,
  body: auth_body,
  headers: {'Content-Type' => 'application/x-www-form-urlencoded',
            'Authorization' => "Basic #{Base64.urlsafe_encode64(auth_client_secret)}"
  }
)
auth_response = auth_request.run
access_token = JSON.parse(auth_response.body)['access_token']
request = Typhoeus::Request.new('https://api.ebay.com/buy/browse/v1/item_summary/search?q=drone&limit=3',
      method: :get,
      headers: {'Authorization' => "Bearer #{access_token}"
    })
response = request.run
puts "|=== #{response.code} ======================="
puts response.body

and this is the response I'm getting:

|=== 401 =======================
{
  "errors" : [ {
    "errorId" : 1001,
    "domain" : "OAuth",
    "category" : "REQUEST",
    "message" : "Invalid access token",
    "longMessage" : "Invalid access token. Check the value of the Authorization HTTP request header."
  } ]
}

When I check the access_token variable it appears to be a valid token. Does it need to be encoded in any way?


Thanks in advance.

authentication failedruby
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

jm1383 avatar image
0 Likes"
jm1383 answered ·

:facepalm: you are 100% correct.

·
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

sagar_developersupport avatar image
0 Likes"
sagar_developersupport answered ·

Hi @jm1383 ,

We have noticed that you are generating client credentials grant type token (Application access token) for sandbox environment while you are making search call of Browse API for production environment.

We suggest to use Application access token generated for sandbox environment, only to make calls on sandbox environment.
If you want to make call in production environment, please generate a Application access token for production environment then use the token in your request.

Best regards ,
eBay Developer Support

· 1 ·
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

thanks a lot. it resolved my issue

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.