Google Auth 在 expo 49 中

huangapple go评论69阅读模式
英文:

Google Auth in expo 49

问题

我正在跟随两个月前的教程学习如何在我的 Expo 应用中使用 google-auth。
关于 Expo-CLI 的一些内容已经过时,但我设法学会了如何使用 ESA-CLI。

当我运行 web 时,一切都正常,整个身份验证流程。
当我尝试 Android 或 iOS 时,无论哪个,都会在尝试进行身份验证后出现相同的错误:

错误 400: url_redirect_mismatch

我查看了这个问题/答案:
https://stackoverflow.com/questions/75763469/expo-auth-google-to-expo-auth-session
并按照答案上的说明操作,

import { makeRedirectUri } from 'expo-auth-session'
...

const [request, response, promptAsync] = Google.useAuthRequest({
    ...googleConfig,
    redirectUri: makeRedirectUri(),
  })

但我得到了不同的错误:

错误 400: invalid_request
请求详情:redirect_uri=expo-google-auth://

我不知道如何修复它,任何帮助都将不胜感激。

英文:

I'm following a tutorial from 2 months ago for how to use google-auth, on my expo app.
there were a few outdated things, related to Expo-CLI changes, but i managed to learn how to use ESA-CLI for that.

Everything works when i run web, the whole authentication flow.
When i try Android or iOS, i get the same error in both, after trying to authenticate:

Error 400: url_redirect_mismatch

I landed on this question / answer:
https://stackoverflow.com/questions/75763469/expo-auth-google-to-expo-auth-session
and followed the instructions on the answer,

import { makeRedirectUri } from 'expo-auth-session'
...

const [request, response, promptAsync] = Google.useAuthRequest({
    ...googleConfig,
    redirectUri: makeRedirectUri(),
  })

and i got a different error:

Error 400: invalid_request
Request details: redirect_uri=expo-google-auth://

I have no idea how to fix it and any help would be appreciated

答案1

得分: 2

这对我有效。

import { makeRedirectUri } from 'expo-auth-session';
import Constants from 'expo-constants';
import * as Google from 'expo-auth-session/providers/google';

const EXPO_REDIRECT_PARAMS = {
  useProxy: true,
  projectNameForProxy: '@yourUsername/yourScheme',
};

const NATIVE_REDIRECT_PARAMS = { native: 'yourScheme://' };

const REDIRECT_PARAMS =
  Constants.appOwnership === 'expo'
    ? EXPO_REDIRECT_PARAMS
    : NATIVE_REDIRECT_PARAMS;

const GOOGLE_CONFIG = {
  androidClientId: '...',
  webClientId: '...',
  iosClientId: '...',
  redirectUri: makeRedirectUri(REDIRECT_PARAMS),
};

const LoginPage = () => {
     const [request, response, promptAsync] = Google.useAuthRequest(GOOGLE_CONFIG)
     ...
}
英文:

this is working for me

import { makeRedirectUri } from 'expo-auth-session';
import Constants from 'expo-constants';
import * as Google from 'expo-auth-session/providers/google';

const EXPO_REDIRECT_PARAMS = {
  useProxy: true,
  projectNameForProxy: '@yourUsername/yourScheme',
};

const NATIVE_REDIRECT_PARAMS = { native: 'yourScheme://' };

const REDIRECT_PARAMS =
  Constants.appOwnership === 'expo'
    ? EXPO_REDIRECT_PARAMS
    : NATIVE_REDIRECT_PARAMS;

const GOOGLE_CONFIG = {
  androidClientId: '...',
  webClientId: '...',
  iosClientId: '...',
  redirectUri: makeRedirectUri(REDIRECT_PARAMS),
};

const LoginPage = ()=> {
     const [request, response, promptAsync] = Google.useAuthRequest(GOOGLE_CONFIG)
     ...

}

huangapple
  • 本文由 发表于 2023年7月10日 16:08:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651842.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定