sun*_*iwo 5 react-native google-signin expo
我正在尝试使用 expo-auth-session 并按照此处的指南实施 Google Signin 。在我的 GenyMotion 模拟器中的 Expo Client 中一切正常。我为 Android 独立应用程序进行了配置,并构建了一个 apk 在实际设备上进行测试。在我的手机上,按下“登录”按钮后,会启动一个浏览器,让我选择要使用的 Google 帐户。但在那之后,浏览器关闭,返回到应用程序,什么也没有发生。修复将受到热烈欢迎。这是我编写的第二个示例代码,用于测试一个全新的项目:
import { StatusBar } from 'expo-status-bar';
import React, { useState, useEffect } from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
import axios from 'axios';
import * as WebBrowser from 'expo-web-browser';
import * as Google from 'expo-auth-session/providers/google';
WebBrowser.maybeCompleteAuthSession();
export default function App() {
const [gUser, setGUser] = useState(null);
const [reqError, setReqError] = useState('');
const [request, response, promptAsync] = Google.useAuthRequest({
expoClientId: 'xxxxxxxxxxxx.apps.googleusercontent.com',
androidClientId: 'xxxxxxxxxxx.apps.googleusercontent.com',
// iosClientId: 'GOOGLE_GUID.apps.googleusercontent.com',
// webClientId: 'GOOGLE_GUID.apps.googleusercontent.com',
});
useEffect(() => {
if (response?.type === 'success') {
const { authentication } = response;
getGoogleUser(authentication.accessToken)
}
}, [response]);
const getGoogleUser = async (accessToken) => {
try{
let gUserReq = await axios.get('https://www.googleapis.com/oauth2/v2/userinfo',
{
headers: {
Authorization: `Bearer ${accessToken}`
}
}
);
console.log(gUserReq.data);
setGUser(gUserReq.data);
}
catch(error){
console.log('GoogleUserReq error: ', error);
setReqError(error);
}
}
return (
<View style={styles.container}>
{
reqError !== '' &&
<View>
<Text>There was an error</Text>
<Text>{JSON.stringify(reqError, 'reqEr', 4)}</Text>
</View>
}
<Text style={{
fontWeight: 'bold'
}}>Signed user</Text>
{
gUser === null &&
<Text>No user</Text>
}
{
gUser !== null &&
<Text>{JSON.stringify(gUser, null, 4)}</Text>
}
<Button
disabled={!request}
title="Sign in"
onPress={() => promptAsync()}
/>
<StatusBar style="auto" />
</View>
);
}
Run Code Online (Sandbox Code Playgroud)
这里是世博诊断输出:
Expo CLI 3.27.13 environment info:
System:
OS: Linux 5.4 Ubuntu 20.04.1 LTS (Focal Fossa)
Shell: 5.0.17 - /bin/bash
Binaries:
Node: 10.19.0 - /usr/bin/node
npm: 6.14.4 - /usr/bin/npm
npmPackages:
expo: ^38.0.0 => 38.0.10
react: 16.13.1 => 16.13.1
react-dom: 16.13.1 => 16.13.1
react-native: https://github.com/expo/react-native/archive/sdk-38.0.2.tar.gz => 0.62.2
react-native-web: ~0.13.12 => 0.13.18
npmGlobalPackages:
expo-cli: 3.27.13
Expo Workflow: managed
Run Code Online (Sandbox Code Playgroud)
提前致谢。
所以,看起来这是expo-auth-session包的一个错误,我不确定为什么,但这里的情况可能会导致这个问题:
setFullResult(fullResult ?? result);
Run Code Online (Sandbox Code Playgroud)
将其更改为:
setFullResult(result);
Run Code Online (Sandbox Code Playgroud)
似乎可以为我解决它。如果该修复程序适合您,您可以使用补丁包
我还提出了一个关于此问题的问题,因为我相信这是一个错误 https://github.com/expo/expo/issues/11714
编辑:
您还需要将结果添加为包含以下效果的依赖项setFullResult
| 归档时间: |
|
| 查看次数: |
992 次 |
| 最近记录: |