我目前正在尝试开发一个使用 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.js 中返回 403 的原因。我一直在寻找来自浏览器、curl 和节点的请求的差异,但根本没有任何线索......
有谁知道可能发生什么?