GoogleAPIs 与 React Native / Expo 的集成
GoogleAPIs integration with React Native / Expo
我正在尝试创建一个 React Native / Expo 应用程序,我可以在其中使用 YouTube Data V3 API 的(视频插入),但我很难理解如何使用 fetch()
通过 Google oauth2 同意屏幕对 google 用户进行身份验证,React Native 获得 access_key
。
到目前为止,这是我的代码:
fetch(
'https://accounts.google.com/o/oauth2/v2/auth?' +
'client_id=' + clientId + '&' +
'redirect_uri=http://localhost:19006&' +
'response_type=token&' +
'scope=https://www.googleapis.com/auth/drive.metadata.readonly&' +
'include_granted_scopes=true&' +
'state=pass-through value',
{
method: 'GET'
}
);
我在 Chrome 控制台中收到 from origin 'http://localhost:19006' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
和 Uncaught (in promise) TypeError: Failed to fetch
错误。如果我将 link 复制并粘贴到浏览器的新选项卡中,则同意页面正在运行。
我使用 Javascript 代码让它工作,但在 React Native 中我无法让它工作。我正在关注 https://developers.google.com/identity/protocols/oauth2/javascript-implicit-flow#js-client-library
上的文档
我在 GitHub 和 Whosebug 上搜索了可能的解决方案,但我找不到 React Native/Expo。我也尝试了 Node.Js Googleapis client library 但据我了解它与 React Native/Expo.
不兼容
您可以尝试使用以下 npm 包或像下面这样为 AppAuth react-native 搜索相关的 npm 包:
https://github.com/FormidableLabs/react-native-app-auth
另请查看文档或示例:
https://developer.okta.com/blog/2018/03/16/build-react-native-authentication-oauth-2
我正在尝试创建一个 React Native / Expo 应用程序,我可以在其中使用 YouTube Data V3 API 的(视频插入),但我很难理解如何使用 fetch()
通过 Google oauth2 同意屏幕对 google 用户进行身份验证,React Native 获得 access_key
。
到目前为止,这是我的代码:
fetch(
'https://accounts.google.com/o/oauth2/v2/auth?' +
'client_id=' + clientId + '&' +
'redirect_uri=http://localhost:19006&' +
'response_type=token&' +
'scope=https://www.googleapis.com/auth/drive.metadata.readonly&' +
'include_granted_scopes=true&' +
'state=pass-through value',
{
method: 'GET'
}
);
我在 Chrome 控制台中收到 from origin 'http://localhost:19006' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
和 Uncaught (in promise) TypeError: Failed to fetch
错误。如果我将 link 复制并粘贴到浏览器的新选项卡中,则同意页面正在运行。
我使用 Javascript 代码让它工作,但在 React Native 中我无法让它工作。我正在关注 https://developers.google.com/identity/protocols/oauth2/javascript-implicit-flow#js-client-library
上的文档我在 GitHub 和 Whosebug 上搜索了可能的解决方案,但我找不到 React Native/Expo。我也尝试了 Node.Js Googleapis client library 但据我了解它与 React Native/Expo.
不兼容您可以尝试使用以下 npm 包或像下面这样为 AppAuth react-native 搜索相关的 npm 包:
https://github.com/FormidableLabs/react-native-app-auth
另请查看文档或示例:
https://developer.okta.com/blog/2018/03/16/build-react-native-authentication-oauth-2