简体   繁体   English

Node中的Reddit API invalid_grant错误

[英]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}`);
          });
        });

Thanks to a reddit user, I discovered my callback URI was getting encoded twice. 感谢reddit用户,我发现我的回调URI得到了两次编码。 Changing to this fixed it... 改成固定的...

const URI= 'http://localhost:8080/api/reddit/callback';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google API invalid_grant - 错误的请求错误 - Google API invalid_grant - bad request error OAuth2:Discord API 总是响应 {“error”: “invalid_grant”} - OAuth2: Discord API always responds with {“error”: “invalid_grant”} 如何修复谷歌 API 'invalid_grant' 错误 - How to fix Google API 'invalid_grant' error Google OAuth2 invalid_grant错误 - Google OAuth2 invalid_grant error Docusign Node.js客户端的invalid_grant错误 - invalid_grant error with docusign nodejs client Google Api 服务帐户“错误”:“invalid_grant”,“error_description”:“JWT 签名无效。” - Google Api Service Account "error":"invalid_grant","error_description":"Invalid JWT Signature." Google Api、Node.js - invalid_grant 格式错误的授权码 - Google Api, Node.js - invalid_grant Malformed auth code 调用Google Drive API会返回Invalid_grant OAuth2 - Calling Google Drive API returns Invalid_grant OAuth2 Uber API-使用非开发人员登录时为invalid_grant - Uber API - invalid_grant when using non developer login WebapiAuthenticationError: An authentication error occurred while communicating with Spotify's Web API. 详细信息:invalid_grant Invalid redirect URI - WebapiAuthenticationError: An authentication error occurred while communicating with Spotify's Web API. Details: invalid_grant Invalid redirect URI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM