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 = eprocess.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': mshop_id]
},
body: {
'upgrade_code': upgrade_code,
'partner_id': process.env.CLIENT_ID,
'shop_id_list': Xshop_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.