如何在 React Native 中登录 YouTube 帐户以访问私人播放列表

问题描述 投票:0回答:3

我正在构建一个 React Native 应用程序,我需要能够查看和播放 YouTube 播放列表。

我已获得 API 密钥,并且可以通过其 ID 成功播放公共 YouTube 视频。

我不知道从哪里开始获取 OAth2 凭据才能访问用户的播放列表。

javascript react-native oauth-2.0 youtube-data-api
3个回答
2
投票

没有任何特定的模块可以让 React Native 登录 YouTube,但您可以查看 YouTube v3 文档中的 Using OAuth 2.0 for JavaScript Web Applications,并检查 client side web apps 部分。一旦获得所需的凭据,检索播放列表就非常简单了。

如果您正在寻找第三方库,您可以查看此


1
投票

react-native-google-signin/google-signin
库很容易与 React Native 项目一起使用来实现 Google 登录。该软件包有一个 Google 登录按钮组件/方法来获取用户的身份验证令牌。

使用步骤:

  1. 安装库:可以通过npm或yarn安装:

    npm install @react-native-google-signin/google-signin
    

    yarn add @react-native-google-signin/google-signin
    
  2. 链接库:如果您使用低于 0.60 的 React Native 版本,则需要使用

    react-native link
    链接库。对于 React Native 0.60 及以上版本,该库会自动链接。

  3. Google Sign-In 项目:您需要在 Google Cloud Platform 控制台上创建一个项目,启用 Google Sign-In API。这是为了获取网络客户端id。

  4. 配置库:使用 Web 客户端 id 到

    GoogleSignin.configure()
    方法:

    import { GoogleSignin } from '@react-native-google-signin/google-signin';
    
    GoogleSignin.configure({
      webClientId: '<Your web client id>',
    });
    
  5. 使用

    GoogleSignin
    方法登录用户并获取用户的身份令牌。

请参阅该库的 GitHub 页面,了解更详细的说明和示例。


-1
投票

开始使用 youtube oauth 的页面是这里

var GoogleAuth; // Google Auth object.
function initClient() {
  gapi.client.init({
      'apiKey': 'YOUR_API_KEY',
      'clientId': 'YOUR_CLIENT_ID',
      'scope': 'https://www.googleapis.com/auth/youtube.force-ssl',
      'discoveryDocs': ['https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest']
  }).then(function () {
      GoogleAuth = gapi.auth2.getAuthInstance();

      // Listen for sign-in state changes.
      GoogleAuth.isSignedIn.listen(updateSigninStatus);
  });
}

初始化后,您需要获得用户等的许可,这在文档中有显示。

© www.soinside.com 2019 - 2024. All rights reserved.