【发布时间】:2022-12-30 17:55:00
【问题描述】:
我正在使用 react native expo 创建一个应用程序,它允许最终用户通过他们的谷歌帐户登录,然后应用程序尝试保存 access_token 以便基于服务器的应用程序可以使用它代表他们发送电子邮件,
但是当使用 google sing in 时,我没有获得刷新令牌并且无法发送电子邮件,
这是我正在使用的代码示例
我尝试了以下方法来获取访问请求
const [request, response, promptAsync] = Google.useIdTokenAuthRequest({
clientId: "XXXXXXX",
androidClientId:"XXXXXXX",
iosClientId:"XXXXXXX"
});
const [initializing, setInitializing] = useState(true);
const [user, setUser] = useState();
const sendNotification=useNotification()
//console.log(sendNotification)
useEffect(() => {
if (response?.type === "success") {
const { id_token } = response.params;
const auth = getAuth();
const credential = GoogleAuthProvider.credential(id_token);
signInWithCredential(auth, credential);
let decoded = jwt_decode(id_token);
socialLogin(decoded)
}
}, [response]);
并在服务器上使用此代码发送电子邮件
const { google } = require('googleapis');
const path = require('path');
const fs = require('fs');
const credentials = require('./credentials.json');
// Replace with the code you received from Google
const code = 'XXXXXXX';
//const code="XXXXXXX"
const { client_secret, client_id, redirect_uris } = credentials.installed;
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);
oAuth2Client.getToken(code).then(({ tokens }) => {
console.log('first')
const tokenPath = path.join(__dirname, 'token.json');
fs.writeFileSync(tokenPath, JSON.stringify(tokens));
console.log('Access token and refresh token stored to token.json');
}).catch(err=>console.log(err));
【问题讨论】:
标签: react-native expo google-oauth google-signin google-email-settings-api