Hig*_*mve 6 android ios google-signin
我正在使用这个lib https://github.com/devfd/react-native-google-signin/建立一个使用react-native的google标志流.
lib工作得很好,我可以成功登录谷歌,但我们需要离线访问api,我们使用这个流程的web应用程序.https://developers.google.com/identity/sign-in/web/server-side-flow.
并且因为Web工作完美,但是当我们尝试在本机应用程序上执行相同操作时,我们在react-native lib中使用该配置.
GoogleSignin.configure({
webClientId: 'the client id of the backend server',
iosClientId: 'the client id of the application',
offlineAccess: true,
forceConsentPrompt: true,
scopes: [
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/plus.me',
'https://www.googleapis.com/auth/gmail.readonly',
'https://www.googleapis.com/auth/pubsub'
]
})
Run Code Online (Sandbox Code Playgroud)
从这里我们得到了来自lib的适当响应,包括:
serverAuthCode: <one-time token to access Google API from the backend on behalf of the user>
Run Code Online (Sandbox Code Playgroud)
但是当我们尝试交换该代码时:
const google = require('googleapis');
const OAuth2 = google.auth.OAuth2;
const oauth2Client = new OAuth2(
process.env.GOOGLE_CLIENT_ID,
process.env.GOOGLE_CLIENT_SECRET,
'postmessage'
);
export function getToken (code: string): Promise<any> {
return new Promise((resolve, reject) => {
oauth2Client.getToken(code, (err, tokens) => {
if (!err) {
resolve(tokens);
} else {
reject(err);
}
});
});
}
Run Code Online (Sandbox Code Playgroud)
我总是得到和错误的redirect_uri_mismatch或invalid_grant.
在这一点上,我不知道还需要改变什么.也许有人知道这里发生了什么.
好的,我找到了解决方案。而且很容易。
当您使用 ServerAuthCode 来交换用户令牌时,您需要将后端的返回 URI 设置为 null。
这就是我最后用 getToken 方法所做的。现在一切都像魅力一样!
export function getToken (code, typeOf = 'web') {
const redirectUri = (typeOf === 'movil') ? null : 'postmessage';
const oauth2Client = new OAuth2(
process.env.GOOGLE_CLIENT_ID,
process.env.GOOGLE_CLIENT_SECRET,
redirectUri
);
return new Promise((resolve, reject) => {
oauth2Client.getToken(code, (err, tokens) => {
if (!err) {
resolve(tokens);
} else {
reject(err);
}
});
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
809 次 |
| 最近记录: |