【问题标题】:Simply extract analytical data from Spotify API只需从 Spotify API 中提取分析数据
【发布时间】:2020-02-03 22:26:53
【问题描述】:

我正在为我的一个项目使用 CRA 和 Express 服务器,该应用程序显示有关某些艺术家及其曲目/专辑的统计信息...

我很难弄清楚如何从 Spotify 获取简单数据。那里的每个教程都希望涉及登录和授权;我不需要这些,只需要关于目前最热门曲目的纯粹统计数据。

我确实设置了我的 Spotify 开发者帐户,并且我已经创建了一个项目。
谁能给我举例说明我正在努力实现的目标?

【问题讨论】:

    标签: node.js reactjs api express spotify


    【解决方案1】:

    要访问任何与 api 相关的服务,您需要为您发出的请求提供 apiKey

    【讨论】:

      【解决方案2】:

      This 包含 Spotify 的所有三个授权流程的示例。

      你想看看Client Credentials flow。此流程仅在客户端工作,不需要任何后端部分。使用Client Credentials flow,您将无法检索特定用户的特定数据,这在您的场景中是不必要的。

      我假设您创建的 project 是一个 Spotify Web API 应用程序,它有一个客户端 ID 和一个客户端密码。需要这两个值才能使Client Credentials flow 正常工作。

      以下 sn-p 显示了 Spotify 的 Client Credentials flow 示例。

      /**
       * This is an example of a basic node.js script that performs
       * the Client Credentials oAuth2 flow to authenticate against
       * the Spotify Accounts.
       *
       * For more information, read
       * https://developer.spotify.com/web-api/authorization-guide/#client_credentials_flow
       */
      
      var request = require('request'); // "Request" library
      
      var client_id = 'CLIENT_ID'; // Your client id
      var client_secret = 'CLIENT_SECRET'; // Your secret
      
      // your application requests authorization
      var authOptions = {
        url: 'https://accounts.spotify.com/api/token',
        headers: {
          'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64'))
        },
        form: {
          grant_type: 'client_credentials'
        },
        json: true
      };
      
      request.post(authOptions, function(error, response, body) {
        if (!error && response.statusCode === 200) {
      
          // use the access token to access the Spotify Web API
          var token = body.access_token;
          var options = {
            url: 'https://api.spotify.com/v1/users/jmperezperez',
            headers: {
              'Authorization': 'Bearer ' + token
            },
            json: true
          };
          request.get(options, function(error, response, body) {
            console.log(body);
          });
        }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-08-22
        • 2015-03-10
        • 2014-04-28
        • 1970-01-01
        • 1970-01-01
        • 2017-04-23
        • 1970-01-01
        相关资源
        最近更新 更多