質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
cURL

cURLはHTTP, FTPやTelnetなど複数のプロトコルを用いてデータを転送するライブラリとコマンドラインツールを提供します。

Access

Accessはマイクロソフトによるリレーショナルデータベース管理システムです。オブジェクト指向のアプリケーション作成に対応しており、テーブルや編集をはじめ、クエリ生成、入力フォーム作成、レポート作成など一通りの機能を備えています。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

Q&A

0回答

654閲覧

Spotify Web APIのアクセストークンをcurlで取りたい

DrqYuto

総合スコア432

cURL

cURLはHTTP, FTPやTelnetなど複数のプロトコルを用いてデータを転送するライブラリとコマンドラインツールを提供します。

Access

Accessはマイクロソフトによるリレーショナルデータベース管理システムです。オブジェクト指向のアプリケーション作成に対応しており、テーブルや編集をはじめ、クエリ生成、入力フォーム作成、レポート作成など一通りの機能を備えています。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

0グッド

0クリップ

投稿2019/06/21 12:03

curl -X "POST" -H "Authorization: Basic e2...Z9" \ -d grant_type=client_credentials https://accounts.spotify.com/api/token

% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 92 100 63 100 29 364 167 --:--:-- --:--:-- --:--:-- 531

となります。アクセストークが見つかりません。

curl -v -X "POST" -H "Authorization: Basic e2...Z9" \ -d grant_type=client_credentials https://accounts.spotify.com/api/token

-vすると、

Note: Unnecessary use of -X or --request, POST is already inferred.
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying 104.199.240.211...

  • TCP_NODELAY set
  • Connected to accounts.spotify.com (104.199.240.211) port 443 (#0)
  • ALPN, offering h2
  • ALPN, offering http/1.1
  • successfully set certificate verify locations:
  • CAfile: /etc/ssl/certs/ca-certificates.crt

CApath: /etc/ssl/certs
} [5 bytes data]

  • (304) (OUT), TLS handshake, Client hello (1):

} [512 bytes data]

  • (304) (IN), TLS handshake, Server hello (2):

{ [109 bytes data]

  • NPN, negotiated HTTP2 (h2)

{ [5 bytes data]

  • TLSv1.2 (IN), TLS handshake, Certificate (11):

{ [2481 bytes data]

  • TLSv1.2 (IN), TLS handshake, Server key exchange (12):

{ [333 bytes data]

  • TLSv1.2 (IN), TLS handshake, Server finished (14):

{ [4 bytes data]

  • TLSv1.2 (OUT), TLS handshake, Client key exchange (16):

} [70 bytes data]

  • TLSv1.2 (OUT), TLS change cipher, Client hello (1):

} [1 bytes data]

  • TLSv1.2 (OUT), TLS handshake, Unknown (67):

} [36 bytes data]

  • TLSv1.2 (OUT), TLS handshake, Finished (20):

} [16 bytes data]

0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* TLSv1.2 (IN), TLS handshake, Finished (20):
{ [16 bytes data]

  • SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
  • ALPN, server did not agree to a protocol
  • Server certificate:
  • subject: C=SE; L=Stockholm; O=Spotify AB; CN=*.spotify.com
  • start date: May 16 00:00:00 2017 GMT
  • expire date: Jul 29 12:00:00 2020 GMT
  • subjectAltName: host "accounts.spotify.com" matched cert's "*.spotify.com"
  • issuer: C=US; O=DigiCert Inc; CN=DigiCert SHA2 Secure Server CA
  • SSL certificate verify ok.
  • Using HTTP2, server supports multi-use
  • Connection state changed (HTTP/2 confirmed)
  • Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0

} [5 bytes data]

  • Using Stream ID: 1 (easy handle 0x55a10a14a580)

} [5 bytes data]

POST /api/token HTTP/2
Host: accounts.spotify.com
User-Agent: curl/7.58.0
Accept: /
Authorization: Basic e2Y2M2ZmMDUzOTQyMTRmY2ZhMWE1MmQ1MDdmODk4ZjUxfTp7MWRlNWI1NmIyYjEyNDdhM2JkNTk2NmM5ZWJlZDNhMDZ9
Content-Length: 29
Content-Type: application/x-www-form-urlencoded

{ [5 bytes data]

  • Connection state changed (MAX_CONCURRENT_STREAMS updated)!

} [5 bytes data]

  • We are completely uploaded and fine

{ [5 bytes data]
< HTTP/2 400
< server: nginx
< date: Fri, 21 Jun 2019 12:00:01 GMT
< content-type: application/json
< content-length: 63
<
{ [63 bytes data]

100 92 100 63 100 29 351 162 --:--:-- --:--:-- --:--:-- 511

  • Connection #0 to host accounts.spotify.com left intact

となり、アクセストークンが帰ってきません。

参考

Spotify Web APIの使い方 | DevelopersIO
https://dev.classmethod.jp/etc/about-using-of-spotify-api/

Client Credentials Flowでアクセストークンを取得
まずは、Client IDとClient SecretをBase64でエンコードした値を取得します。

Macならばターミナル上で下記を実行します。

echo -n {client_id}:{Client_Secret} | base64

出力された値を「ZjM4ZjAw...WY0MzE=」とした場合、以下のようにリクエストを実行します。

curl -X "POST" -H "Authorization: Basic ZjM4ZjAw...WY0MzE=" \ <br />-d grant_type=client_credentials https://accounts.spotify.com/api/token

以下のような結果が返ってくるので、アクセストークンを控えておきます。

{
"access_token": "NgCXRKc...MzYjw",
"token_type": "Bearer",
"expires_in": 3600,
"scope": ""
}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問