简体   繁体   English

Google OAuth2 + react-google-login:尝试检索刷新令牌时出现“redirect_uri_mismatch”

[英]Google OAuth2 + react-google-login: "redirect_uri_mismatch" when trying to retrieve a refresh token

how are you doing?你好吗?

I'm using react-google-login to make the login flow in the frontend.我正在使用react-google-login在前端进行登录流程。

This is my login button setup:这是我的登录按钮设置:

<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>

This is working fine and I'm successfully receiving an authorization_code from Google.这工作正常,我已成功收到来自 Google 的authorization_code码。 The problem is when I try to grab a refresh token for the user.问题是当我尝试为用户获取刷新令牌时。

This is the code I'm using to retrieve the refresh token:这是我用来检索刷新令牌的代码:

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;
    }
}

And this is the error I'm receiving:这是我收到的错误:

{
  "error": "redirect_uri_mismatch",
  "error_description": "Bad Request"
}

My redirect_uri is set as http://localhost:3000/ , and I have confirmed that it is configured correctly in my Google project.我的redirect_uri设置为http://localhost:3000/ ,并且我已经确认它在我的 Google 项目中配置正确。

What am I missing here?我在这里想念什么?

Okay, I finally found a workaround.好的,我终于找到了解决方法。

Setting the redirect_uri as postmessage did the trick.redirect_uri设置为postmessage就行了。

Why?为什么? I'm still wondering... But it works!我仍然想知道......但它有效!

after compiling my app as a standalone apk and regenerating new keytool password i started recieving error 400 uri_mismatch i even went to add accounts.google.com in my authorized uri in the developer google console在将我的应用程序编译为独立的 apk 并重新生成新的 keytool 密码后,我开始收到错误 400 uri_mismatch 我什至在开发人员谷歌控制台的授权 uri 中添加了 accounts.google.com

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM