【问题标题】:Google email offline access using react native expo app使用 React Native Expo 应用程序的谷歌电子邮件离线访问
【发布时间】: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


    【解决方案1】:

    async function signInWithGoogleAsync() { try { const result = await Google.logInAsync({ androidClientId: YOUR_CLIENT_ID_HERE, scopes: ['profile', 'email'], }); if (result.type === 'success') { onSignIn(result);返回结果.accessToken; } else { return { cancelled: true }; } } catch (e) { return { error: true }; } }

    【讨论】:

      猜你喜欢
      • 2022-11-07
      • 1970-01-01
      • 2019-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多