【发布时间】:2020-08-26 16:23:46
【问题描述】:
我在我的 react-native 应用中使用 Google OAuth2 进行身份验证时遇到了一些问题。我正在使用“expo-auth-session”库进行身份验证。我需要获取访问令牌,然后获取 Youtube 个人资料。但我遇到了错误“redirect_uri 的参数值无效:无效方案”
我在 app.json 中的方案:
"scheme": "com.liga.online"
我的代码如下:
import {
makeRedirectUri,
useAuthRequest,
ResponseType,
} from "expo-auth-session";
const discoveryYoutube = {
authorizationEndpoint: 'https://accounts.google.com/o/oauth2/v2/auth',
tokenEndpoint: 'https://oauth2.googleapis.com/token',
revocationEndpoint: 'https://oauth2.googleapis.com/revoke'
};
/// Inside my React component
const [requestYoutube, responseYoutube, promptAsyncYoutube] = useAuthRequest(
{
responseType: ResponseType.Code,
clientId: YOUTUBE_CLIENT_ID,
scopes: ["https://www.googleapis.com/auth/youtube.readonly"],
redirectUri: makeRedirectUri({
native: "com.liga.online/callback",
}),
},
discoveryYoutube
);
当我按下按钮时,回调开始
const signInYoutube = async () => {
const response = await promptAsyncYoutube();
console.log(response.data);
}
知道如何解决吗?
附:我尝试使用库“expo-google-app-auth”修复它。我获得了访问令牌,但是当我尝试获取 Youtube 个人资料并获得“请求失败,状态码为 403”时。
更新 1
顺便说一下我与 Youtube 个人资料的连接。
我改变了一些东西来获取访问令牌。
例如:
import * as Google from 'expo-google-app-auth';
import { startAsync } from 'expo-auth-session';
// Inside my React component
// When I press the button, callback is starting
const signInYoutube = async () => {
const config = {
androidClientId: YOUTUBE_CLIENT_ID
};
const { type, accessToken, user } = await Google.logInAsync(config);
// I need again open my Browser for get Youtube data
const response = await startAsync({
authUrl: `https://www.googleapis.com/youtube/v3/channels?access_token=${accessToken}&part=snippet&mine=true&scope=https://www.googleapis.com/auth/youtube.readonly`,
showInRecents: true
});
console.log(response.data);
}
但我得到错误
更新 2
我想看看从 AuthRequest 加载了哪些数据。我看到非常奇怪的日志。 Redirect_uri 与集合不同。
分辨率
当我在我的范围中添加“https://www.googleapis.com/auth/youtube.readonly”时 - 我可以获得个人资料数据。下面的另一个词是我的代码。
import axios from 'axios';
import * as Google from 'expo-google-app-auth';
// Inside my React component
// Callback function
const signInYoutube = async () => {
const config = {
androidClientId: YOUTUBE_CLIENT_ID,
scopes: ['https://www.googleapis.com/auth/youtube.readonly']
};
const { type, accessToken, user } = await Google.logInAsync(config);
if (type === 'success') {
const response = await axios.get(
`https://www.googleapis.com/youtube/v3/channels?part=id&mine=true&key=${encodeURI(YOUTUBE_API_KEY)}`,
{
headers: {
Authorization: `Bearer ${accessToken}`
}
}
);
setYoutubeData({ accessToken, user, youtubeId: response.data.items[0].id });
}
};
重要
【问题讨论】:
标签: react-native oauth-2.0 google-api youtube-api expo