Best answer

Shopee Integration return "Field named `access_token` not found in OAuth2 results "

  • 3 November 2022
  • 1 reply
  • 621 views

hello everyone, I was working on Shopee integration for work. I’m new to zapier but still trying to make this Shopee integration working. It been a few days I stuck at access token request because it always return Field named `access_token` not found in OAuth2 results error when I try to connect to my app integration.

https://markuphero.com/share/tf0QMUufX3mBlj7Ye4tc

First, I’m not sure what URL should I use for my Authorization URL. I did try to use https://shopee.com.my/ but when I try to connect to my app integration, it send me to shopee main page. Even after I login to my account it keep going like I’m using normal Shopee instead connect to my app Integration. I just decide to use the same URL at “copy your OAuth Redirect URL” because it lead to normal zapier connect app page.
Once again, I’m not used to zapier OAuth2 that why I make this type of decision.

Back to “Access Token Request”, I’m having a hard time trying to make my code work to get access token.
Here link to shopee documentation for Shopee API Reference : https://open.shopee.com/documents/v2/v2.public.refresh_access_token?module=104&type=1
What I find difficult is the API call to get Shopee Access Token is required current refresh token, since I can’t just retrieve current refresh token that I usually store in spreadsheet like I usually did with Google App Script, I use process.env.refresh_token to retrieve refresh token from Refresh Token Request section.

const crypto = z.require('crypto');

var timestamp = Math.round(new Date().getTime() / 1000);

var partner_id = process.env.CLIENT_ID;

var shop_id = process.env.SHOP_ID;

var path = "/api/v2/auth/access_token/get";

var partner_key = process.env.CLIENT_SECRET;

var base_string = partner_id + path + timestamp;

var refresh_token = process.env.refresh_token;

        

        hash = crypto.createHmac('sha256', partner_key).update(base_string).digest('hex');

        

        sign = hash.toUpperCase();

        

          const options2 = {

          url: 'https://partner.shopeemobile.com/api/v2/auth/access_token/get',

          method: 'POST',

          headers: {

            'content-type': 'application/x-www-form-urlencoded',

            'accept': 'application/json',

            'X-TIMESTAMP': timestamp,

            'X-SIGN': sign,

            'X-PARTNER-ID': partner_id,

            'X-REFRESH-TOKEN': refresh_token

          },

          params: {

            'timestamp': timestamp,

            'sign': sign,

            'refresh_token': refresh_token,

            'partner_id': process.env.CLIENT_ID,

          },

          body: {

            'refresh_token': refresh_token,

            'partner_id': process.env.CLIENT_ID,

            'shop_id': shop_id,

            'partner_secret': process.env.CLIENT_SECRET,

            'grant_type': 'authorization_code',

            'redirect_uri': "https://zapier.com/dashboard/auth/oauth/return/App171187CLIAPI/"

          }

        }

        

        return z.request(options2)

      .then((response) => {

        response.throwForStatus();

        const results = response.json;

        

        // You can do any parsing you need for results here before returning them


        return results;

        });

After testing and get Field named `access_token` not found in OAuth2 results, I decide to change my code by calling two different API. One will create a new refresh token, and with that refresh token will be use as request parameter to create new access token
 

//get refresh token only
const crypto = z.require('crypto');
var timestamp = Math.round(new Date().getTime() / 1000);
var partner_id = process.env.CLIENT_ID;
var path = "/api/v2/public/get_refresh_token_by_upgrade_code";
var partner_key = process.env.CLIENT_SECRET;
var base_string = partner_id + path + timestamp;
var upgrade_code = '....';
var shop_id = [process.env.SHOP_ID];

var hash = crypto.createHmac('sha256', partner_key).update(base_string).digest('hex');

var sign = hash.toUpperCase();

const options = {
url: 'https://partner.shopeemobile.com/api/v2/public/get_refresh_token_by_upgrade_code',
method: 'POST',
headers: {
'content-type': 'application/x-www-form-urlencoded',
'accept': 'application/json',
'X-TIMESTAMP': timestamp,
'X-SIGN': sign,
'X-PARTNER-ID': partner_id,
'X-UPGRADE-CODE': upgrade_code,
'X-SHOP-ID-LIST': shop_id
},
params: {
'timestamp': timestamp,
'sign': sign,
'upgrade_code': upgrade_code,
'partner_id': process.env.CLIENT_ID,
'shop_id_list': shop_id
},
body: {
'upgrade_code': upgrade_code,
'partner_id': process.env.CLIENT_ID,
'shop_id_list': shop_id,
'partner_secret': process.env.CLIENT_SECRET,
'grant_type': 'refresh_token',
'redirect_uri': "https://zapier.com/dashboard/auth/oauth/return/App171187CLIAPI/"
}
}

let result = z.request(options)
.then((response) => {
response.throwForStatus();
const results = response.json;
});

var refresh_token = result.refresh_token;

//use previous generated refresh token for get access token

// const crypto = z.require('crypto');
// var timestamp = Math.round(new Date().getTime() / 1000);
// var partner_id = process.env.CLIENT_ID;
// var shop_id = process.env.SHOP_ID;
path = "/api/v2/auth/access_token/get";
// var partner_key = process.env.CLIENT_SECRET;
base_string = partner_id + path + timestamp;
// var refresh_token = '...';

var hash = crypto.createHmac('sha256', partner_key).update(base_string).digest('hex');

var sign = hash.toUpperCase();

const options2 = {
url: 'https://partner.shopeemobile.com/api/v2/auth/access_token/get',
method: 'POST',
headers: {
'content-type': 'application/x-www-form-urlencoded',
'accept': 'application/json',
'X-TIMESTAMP': timestamp,
'X-SIGN': sign,
'X-PARTNER-ID': partner_id,
'X-REFRESH-TOKEN': refresh_token
},
params: {
'timestamp': timestamp,
'sign': sign,
'refresh_token': refresh_token,
'partner_id': process.env.CLIENT_ID,
},
body: {
'refresh_token': refresh_token,
'partner_id': process.env.CLIENT_ID,
'shop_id': shop_id,
'partner_secret': process.env.CLIENT_SECRET,
'grant_type': 'access_token', //authorization_code
'redirect_uri': "https://zapier.com/dashboard/auth/oauth/return/App171187CLIAPI/"
}
}

return z.request(options2)
.then((response) => {
response.throwForStatus();
const info = response.json;

// You can do any parsing you need for results here before returning them

let access_token = info.access_token;
// });

});

 After testing, it still return the same error.
I also have write a code that will create only a new refresh token for Refresh Token Request
 

const crypto = z.require('crypto');
var timestamp = Math.round(new Date().getTime() / 1000);
var partner_id = process.env.CLIENT_ID;
var path = "/api/v2/public/get_refresh_token_by_upgrade_code";
var partner_key = process.env.CLIENT_SECRET;
var base_string = partner_id + path + timestamp;
var upgrade_code = '...';
var shop_id = process.env.SHOP_ID

var hash = crypto.createHmac('sha256', partner_key).update(base_string).digest('hex');

var sign = hash.toUpperCase();

const options = {
url: 'https://partner.shopeemobile.com/api/v2/public/get_refresh_token_by_upgrade_code',
method: 'POST',
headers: {
'content-type': 'application/x-www-form-urlencoded',
'accept': 'application/json',
'X-TIMESTAMP': timestamp,
'X-SIGN': sign,
'X-PARTNER-ID': partner_id,
'X-UPGRADE-CODE': upgrade_code,
'X-SHOP-ID-LIST': [shop_id]
},
params: {
'timestamp': timestamp,
'sign': sign,
'upgrade_code': upgrade_code,
'partner_id': process.env.CLIENT_ID,
'shop_id_list': [shop_id]
},
body: {
'upgrade_code': upgrade_code,
'partner_id': process.env.CLIENT_ID,
'shop_id_list': [shop_id]
'partner_secret': process.env.CLIENT_SECRET,
'grant_type': 'refresh_token',
// 'redirect_uri': bundle.inputData.redirect_uri
}
}

return z.request(options)
.then((response) => {
response.throwForStatus();
const results = response.json;

// You can do any parsing you need for results here before returning them

return results.refresh_token;
});

I’m very sorry if there so many mistake I make in my code, I really new to Zapier and slowly learning how to code with it.
I really hope experts in this community can show me how to create a proper code to solve this problem and show me all the mistake I did for this work.
I’m really appreciate everyone that try to help me solving this problem. 
Thank you.

icon

Best answer by iansco 9 November 2022, 08:11

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

1 reply

Userlevel 2
Badge +1

Hey @louiss lim! 👋

The Field named access token not found in OAuth2 results error means that the API you’re using isn’t returning an access token after the first step of the authentication process, i.e. after the “Authorization URL” is called:

45ff980f09cb942ff7e6bc44072e5532.png

First, I’m not sure what URL should I use for my Authorization URL. I did try to use https://shopee.com.my/ but when I try to connect to my app integration, it send me to shopee main page.

If you’re using https://shopee.com.my as the “Authorization URL” in your app, that’s almost certainly the first problem that will need to be resolved. As you’ve noted, this URL takes you to the homepage for Shopee, it’s not the URL that Shopee uses for authorization via their API.

As next steps, I’d recommend reading the “Authorization and Authentication” page on Shopee’s API documentation as it explains which URLs need to be used and also which parameters need to be sent: https://open.shopee.com/developer-guide/20

You can then set those details up in the “Authorization URL”, “Access Token Request” and “Refresh Token Request” sections of your Zapier app.

If you’re not super familiar with the OAuth 2.0 authorization process, I’d also recommend walking through the “Authorization Code” example on the “OAuth 2.0 Playground” website: https://www.oauth.com/playground/index.html

3443026de69b1d0c28b2e78189f2f6c4.png
Zapier uses the same “Authorization Code” flow, so using the playground to walk through the flow is a great way to get a better understanding of how this process works, which will help you get things setup in your Zapier app.

If you haven’t already done so, I’d also recommend reading through the Zapier documentation for OAuth 2 as it covers each step of the setup process: https://platform.zapier.com/docs/oauth.

Hope this helps get you moving forward!