Instagram API 不断返回"You must provide a valid client_secret and code"错误



在google云功能中,我需要使用instagram API交换代码以获得令牌。

const code = req.body.code;
const config = {
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
};
const prepReq = () => {
return axios.post(
"https://api.instagram.com/oauth/access_token",
qs.stringify({
client_id: "my_insta_app_id",
client_secret: "my_insta_secret",
grant_type: "authorization_code",
redirect_uri: `my_redirect_uri`,
code: code
}),
config
);
};
try {
const response = await prepReq();
if (response.data.access_token) {
return res
.status(200)
.type("application/json")
.send({
success: true,
access_token: response.data.access_token,
user_id: response.data.user_id
});
} else {
throw new Error(
"Something went wrong during authorization. Please try again."
);
}
} catch (e) {
console.log(e);
return res
.status(400)
.type("application/json")
.send({
success: false,
message: e.response.data.error_message
});
}

然而,它仍然返回错误:

data: {
error_type: 'OAuthException',
code: 400,
error_message: 'You must provide a valid client_secret and code'
}

我检查了我的秘密和身份证,结果是正确的。此外,当我使用Postman测试API时,它会返回成功响应。

此外,我不知道为什么,但有时,我的函数会返回正确的响应。

//编辑-张贴整个谷歌功能代码。由于它改变了我传递数据的方式(在axios文档中发现这是一个解决方案,但它也不起作用(,我将其单独发布:


exports.socialAuthorizationHandler = functionsRegion.https.onRequest(
(req, res) => {
cors(req, res, async () => {
const code = req.body.code;
const config = {
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
};
const prms = new test.URLSearchParams({
client_id: "my_insta_app_id",
client_secret: "my_insta_secret",
grant_type: "authorization_code",
redirect_uri: `my_redirect_uri`,
code: code
});
const prepReq = () => {
return axios.post(
"https://api.instagram.com/oauth/access_token/",
prms.toString(),
config
);
};
try {
const response = await prepReq();
if (response.data.access_token) {
return res
.status(200)
.type("application/json")
.send({
success: true,
access_token: response.data.access_token,
user_id: response.data.user_id
});
} else {
throw new Error(
"Something went wrong during authorization. Please try again."
);
}
} catch (e) {
console.log(e);
return res
.status(400)
.type("application/json")
.send({
success: false,
message: e.response.data.error_message
});
}
});
}
);

如文档中所述,要终止HTTP函数,需要对response对象调用以下方法之一:redirect()send()end()

因此,您不需要返回任何内容(就像对任何后台云函数所做的那样(,只需调用其中一个Express.js方法即可。

因此,与其这么做:

exports.socialAuthorizationHandler = functionsRegion.https.onRequest(
(req, res) => {
cors(req, res, async () => {
//...
try {
const response = await prepReq();
if (response.data.access_token) {
return res
.status(200)
.type("application/json")
.send({
success: true,
access_token: response.data.access_token,
user_id: response.data.user_id
});
} else {
// ...
}
} catch (e) {
console.log(e);
return res
.status(400)
.type("application/json")
.send({
success: false,
message: e.response.data.error_message
});
}
});
});

你应该做

exports.socialAuthorizationHandler = functionsRegion.https.onRequest(
(req, res) => {
cors(req, res, async () => {
//...
try {
const response = await prepReq();
if (response.data.access_token) {
res
.status(200)
.type("application/json")
.send({
success: true,
access_token: response.data.access_token,
user_id: response.data.user_id
});
} else {
// ...
}
} catch (e) {
console.log(e);
res
.status(400)
.type("application/json")
.send({
success: false,
message: e.response.data.error_message
});
}
});
});

根据你上面的评论,似乎通过正确地结束云功能,你就不会遇到以前的问题。

相关内容

  • 没有找到相关文章

最新更新