[英]Reddit API invalid_grant error in Node
I am trying to implement OAuth on the reddit API. 我正在尝试在reddit API上实现OAuth。 I request an authorization code, click the link, but then when I try to do a POST to get the token I get "invalid_grant" as my error.
我请求授权码,单击链接,但是当我尝试执行POST以获得令牌时,我收到“ invalid_grant”作为错误。 I read in their docs this means "The code has expired or already been used", but that's not possible because I just requested the code.
我在他们的文档中读过,意思是“代码已过期或已被使用”,但这是不可能的,因为我只是请求了代码。 Any help is appreciated!
任何帮助表示赞赏!
This works fine... 这很好用...
const CLIENT_ID="*****************";
const CLIENT_SECRET="**********************";
const TYPE="code";
const RANDOM_STRING="random_string";
const URI= encodeURIComponent('http://localhost:8080/api/reddit/callback');
const DURATION="temporary";
const SCOPE_STRING="identity";
const GRANT_TYPE='authorization_code';
const router = express.Router();
router.get('/login', function(req, res){
console.log(`Redirecting to: https://www.reddit.com/api/v1/authorize?client_id=${CLIENT_ID}&response_type=${TYPE}&state=${RANDOM_STRING}&redirect_uri=${URI}&duration=${DURATION}&scope=${SCOPE_STRING}`);
res.redirect(`https://www.reddit.com/api/v1/authorize?client_id=${CLIENT_ID}&response_type=${TYPE}&state=${RANDOM_STRING}&redirect_uri=${URI}&duration=${DURATION}&scope=${SCOPE_STRING}`);
});
error is somewhere in here... 错误在这里某处...
router.get('/callback', function(req, res){
if (!req.query.code) throw new Error('NoCodeProvided');
if (req.query.state != RANDOM_STRING) throw new error('Mismatched strings');
const CODE = req.query.code;
request('https://www.reddit.com/api/v1/access_token', {
method: 'POST',
form: {
grant_type: GRANT_TYPE,
code: CODE,
redirect_uri: URI
},
auth: {
username: CLIENT_ID,
password: CLIENT_SECRET
}
}, function (error, res, body){
if (error){
console.log("Error: " + error);
return;
}
console.log(res);
console.log(res.access_token);
console.log(CODE);
//const json = res.json();
//res.redirect(`/?token=${json.access_token}`);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.