can't get access token

Solved
NK160
Tourist
9 0 1

Hi guys,

I'm trying to send to the shop's access_token endpoint once the user confirmed installation and get a bad request error (400).

Here's the code:

 

const storeId = request.query.shop;
 const accessCode = request.query.code;
 const secret = SECRET_KEY;
 const key = API_KEY;
 const url = "https://" + storeId + "/admin/oauth/access_token";
  let body = {
    "client_id": key,
    "client_secret": secret,
    "code": accessCode
  }
  body = JSON.stringify(body);
  return fetch(url, {
   method: "POST",
   headers: {
                "Content-Type": "application/json",
            },
    body: body
  })

 

 
Any ideas? 
Thanks,
0 Likes
NK160
Tourist
9 0 1

Sorry, I've just noticed that a similar question appears in the first page  But anyway there's no answer there. So please let me know what you think.

0 Likes
Greg_Kujawa
Shopify Partner
1016 83 226

The other post on here provides a couple of packages that can do the work for you. Making it easier to work through the access_token acquisition. That's the quickest and easiest way to get started really.

One thing in your example code is that if your body is initially defined as JSON, then you probably don't need the quotes. Maybe try it this way.

 let body = {
    client_id: key,
    client_secret: secret,
    code: accessCode
  }

 

0 Likes
NK160
Tourist
9 0 1

Thanks, From some technical reasons I'm trying to accomplish it without the packages.

And you're right the quotes are not necessary (but it didn't resolve the issue). 

0 Likes
Greg_Kujawa
Shopify Partner
1016 83 226

This is an accepted solution.

If you won't or can't incorporate a third-party library into your project then you probably need to break things down to their simplest form. If it were me, I'd just use CURL or Postman to make the API request. See what the response is. Then I'd trace your own project's API request to see what it's sending over the wire. I always use Fiddler for that to see the raw data being passed.

Here is a Postman request that works. The only header I am providing is Content-Type: application/json. 

 access_token.jpg

NK160
Tourist
9 0 1

Thanks

0 Likes
policenauts
Trailblazer
170 8 29

I agree with Greg, first confirm it works in Postman. 

What environment are you using for your JS code? What is the exact error you're getting? Are you getting a CORS error? 

NK160
Tourist
9 0 1

Thank you all. I figured it out.

0 Likes