【问题标题】:how to use youtube data api v3 parameter mine如何使用 youtube data api v3 参数 mine
【发布时间】:2021-07-28 17:33:22
【问题描述】:

我想使用 youtube data api v3 获取用户的播放列表。

这是 youtube.js

import axios from 'axios';
const KEY = process.env.REACT_APP_API_KEY;

export default axios.create({
    baseURL: 'https://www.googleapis.com/youtube/v3/',
    params: {
        part: 'snippet',
        maxResults: 5,
        key: KEY
    }
})  

这是请求用户播放列表的按钮。 props 是令牌(access_token 使用 oauth2.0)

我想使用 youtube-api 的“mine”参数和这个令牌获取用户的播放列表。

要使用我的参数,我必须发送令牌。

如何发送这个令牌?

const LoadPlaylistButton = ({ token }) => {
    
    const loadPlaylists = async () => {
        const response = await youtube.get('/playlists', {
            params: {
                mine: true
            }
        })
        console.log("log: ", response);
    }
    
    return (
        <button onClick={loadPlaylists}>LoadPlaylist</button>
    );
};
export default LoadPlaylistButton;

【问题讨论】:

    标签: reactjs youtube-api youtube-data-api


    【解决方案1】:

    使用我的将需要您作为用户授权才能请求该数据。 API 密钥只会让您访问公共数据而不是私人数据

    <script src="https://apis.google.com/js/api.js"></script>
    <script>
      /**
       * Sample JavaScript code for youtube.playlists.list
       * See instructions for running APIs Explorer code samples locally:
       * https://developers.google.com/explorer-help/guides/code_samples#javascript
       */
    
      function authenticate() {
        return gapi.auth2.getAuthInstance()
            .signIn({scope: "https://www.googleapis.com/auth/youtube.readonly"})
            .then(function() { console.log("Sign-in successful"); },
                  function(err) { console.error("Error signing in", err); });
      }
      function loadClient() {
        gapi.client.setApiKey("YOUR_API_KEY");
        return gapi.client.load("https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest")
            .then(function() { console.log("GAPI client loaded for API"); },
                  function(err) { console.error("Error loading GAPI client for API", err); });
      }
      // Make sure the client is loaded and sign-in is complete before calling this method.
      function execute() {
        return gapi.client.youtube.playlists.list({})
            .then(function(response) {
                    // Handle the results here (response.result has the parsed body).
                    console.log("Response", response);
                  },
                  function(err) { console.error("Execute error", err); });
      }
      gapi.load("client:auth2", function() {
        gapi.auth2.init({client_id: "YOUR_CLIENT_ID"});
      });
    </script>
    <button onclick="authenticate().then(loadClient)">authorize and load</button>
    <button onclick="execute()">execute</button>
    
    </body>
    </html>
    

    【讨论】:

      【解决方案2】:

      要使用我的,请在标题中设置授权。 检查this

      https://accounts.google.com/o/oauth2/auth?
        client_id=123456789-sdnjknsdkjf4skfnsdjnsfdksdkjl.apps.googleusercontent.com&
        redirect_uri=http%3A%2F%2Flocalhost%2Foauth2callback&
        scope=https://www.googleapis.com/auth/youtube&
        response_type=token
      
       const res = await fetch('https://content-youtube.googleapis.com/youtube/v3/channels
      mine=true&part=statistics&key=asdasdasd', 
      { headers: { Authorization: `Bearer ${token}` } },);
      

      【讨论】:

        猜你喜欢
        • 2013-09-27
        • 2016-09-05
        • 2015-08-23
        • 2015-08-05
        • 1970-01-01
        • 1970-01-01
        • 2014-08-09
        • 2016-10-22
        • 2015-08-10
        相关资源
        最近更新 更多