首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试检索刷新令牌时,Google OAuth2 + react-google-login:“redirect_uri_mismatch

尝试检索刷新令牌时,Google OAuth2 + react-google-login:“redirect_uri_mismatch
EN

Stack Overflow用户
提问于 2021-11-10 22:13:16
回答 1查看 59关注 0票数 1

你好吗?

我使用react-google-login在前台进行登录。

这是我的登录按钮设置:

代码语言:javascript
复制
<GoogleLogin
    accessType="offline"
    clientId={google.client_id}
    cookiePolicy="single_host_origin"
    onSuccess={handleLogin}
    onFailure={handleLoginFail}
    prompt="consent"
    responseType="code"
    scope="email profile https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/calendar.events.readonly openid"
    render={(_props) => (
        <Button className="google-login-btn" onClick={_props.onClick}>
            {props.buttonText} <GoogleIcon className="google-icon" height="20px" width="20px" />
        </Button>
    )}>
</GoogleLogin>

这工作得很好,我成功地收到了来自谷歌的authorization_code。问题是当我试图为用户获取刷新令牌时。

这是我用来检索刷新令牌的代码:

代码语言:javascript
复制
const { client_id, client_secret, redirect_uri } = require('../config').google;

export async function GetRefreshToken(code) {
    try {
        const data = new URLSearchParams({
            code,
            client_id,
            client_secret,
            grant_type: 'authorization_code',
            redirect_uri
        });

        const headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
        };

        const response = await axios.post('https://oauth2.googleapis.com/token', data, { headers });
        const parsedResponse = JSON.parse(response.data);

        if (parsedResponse.refresh_token) return parsedResponse;
        else return null;
    } catch (err) {
        console.error(err);
        throw err;
    }
}

这是我收到的错误:

代码语言:javascript
复制
{
  "error": "redirect_uri_mismatch",
  "error_description": "Bad Request"
}

我的redirect_uri设置为http://localhost:3000/,并且我已经确认它在我的Google项目中配置正确。

这里我漏掉了什么?

EN

回答 1

Stack Overflow用户

发布于 2021-11-20 06:39:00

好吧,我终于找到了一个变通办法。

redirect_uri设置为postmessage可以做到这一点。

为什么?我还在想..。但是它起作用了!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69920832

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档