[英]Oauth2 GET request with access token returns 403 in NodeJS
我目前正在嘗試開發使用DeviantArt API的節點應用程序。
所有請求都必須通過Oauth2,因此我決定使用client_credentials流,因為代碼將全部私有。
因此,一旦我在DeviantArt中注冊了我的應用程序,便得到了我的clientId和clientSecret。
我正在使用client-oauth2軟件包進行授權,代碼如下所示:
const Oauth2 = require('client-oauth2');
...
function auth() {
return new Promise((resolve, reject) => {
let auth = new Oauth2({
clientId: KEYS.client_id,
clientSecret: KEYS.client_secret,
accessTokenUri: AUTH_URL, // https://www.deviantart.com/oauth2/token
authorizationGrants: ['credentials']
});
auth.credentials.getToken()
.then((data) => {
resolve({
token: data.accessToken
});
})
.catch(reject);
});
}
到目前為止,這已經可以正常工作了,我正在獲取access_token 。 因此,我可以使用此令牌在API上執行任何請求,並且它實際上可以通過curl和瀏覽器使用:
https://www.deviantart.com/api/v1/oauth2/browse/newest?access_token={{access_token}}
回到我的節點應用程序中,我正在使用請求包,代碼如下所示:
const request= require('request');
...
function getDeviations(token) {
return new Promise((resolve, reject) => {
request(`https://www.deviantart.com/api/v1/oauth2/browse/newest?
access_token=${token}`, (error, response, body) => {
if (error || response.statusCode >= 400) {
reject({
error,
response
});
} else {
// do things and resolve
}
});
});
}
並返回403 Forbidden 。
我已經花了兩天時間將頭撞在鍵盤上,以尋找僅返回node才能返回403的原因。 我一直在尋找來自瀏覽器,curl和node的請求中的差異,但一點都不知道...
有人知道可能會發生什么嗎?
我明白了...偽造標題中的用戶代理...
function getDeviations(token) {
return new Promise((resolve, reject) => {
request({
url: `https://www.deviantart.com/api/v1/oauth2/browse/newest?
access_token=${token}`,
headers: {
'User-Agent': 'curl/7.44.0'
}
}, (error, response, body) => {
if (error || response.statusCode >= 400) {
reject({
error,
response
});
} else {
// do things and resolve
}
});
});
}
我不敢相信這行得通。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.