2016-04-04 15 views
0

Spotify APIからアクセストークンを取得しようとしていますが、取得できません。私は500の内部サーバーエラーを取得しているときに、認証コードを取得してからプロセスを実行することができます。ここに私のコードです:Spotify APIトークン検索で500の内部サーバーエラーが発生する

using System; 
using System.Collections.Generic; 
using System.Diagnostics; 
using System.IO; 
using System.Linq; 
using System.Net; 
using System.Text; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace SpotifyAPI 
{ 
    public partial class Callback : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      string code = Request.QueryString["code"]; 
      string state = Request.QueryString["state"]; 

      // Generate URL with authorization code 
      string url = "https://accounts.spotify.com/api/token?grant_type=authorization_code&code=" + code + "&redirect_uri=http://localhost:64270/Callback.aspx"; 

      // get Client ID and Secret 
      string clientId = ((Site1)this.Master).getClientId(); 
      string clientSecret = ((Site1)this.Master).getClientSecret(); 

      // generate encoded header string for token request 
      string preAuthorizationCode = clientId + ":" + clientSecret; 
      string postAuthorizationCode = Base64Encode(preAuthorizationCode); 
      Debug.WriteLine("Post Authorization Code is: " + postAuthorizationCode); 

      var webrequest = (HttpWebRequest)WebRequest.CreateHttp(url); 
      webrequest.Method = "POST"; 

      WebHeaderCollection myWebHeaderCollection = webrequest.Headers; 
      myWebHeaderCollection.Add("Authorization: Basic " + postAuthorizationCode); 

      HttpWebResponse myHttpWebResponse = (HttpWebResponse)webrequest.GetResponse(); 

      Stream responseStream = myHttpWebResponse.GetResponseStream(); 
      var reader = new StreamReader(responseStream); 
      string result = reader.ReadToEnd(); 
     } 

     public static string Base64Encode(string plainText) 
     { 
      var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText); 
      return System.Convert.ToBase64String(plainTextBytes); 
     } 
    } 
} 

私は助けていただきありがとうございます。ありがとう!ドキュメントあなたは

"grant_type"、 "コード" を送信し、POSTのボディなどのパラメータを "REDIRECT_URI" する必要が

https://developer.spotify.com/web-api/authorization-guide/

で述べたように

+0

SpotifyのPOSTまでになるのですか、それとも途切れる行ですか? –

+0

この行の前と後の行程で行が途切れています:HttpWebResponse myHttpWebResponse =(HttpWebResponse)webrequest.GetResponse(); – Arrivedacci

答えて

0

。 Spotifyがエラーを返すようにそれらをurlパラメータとして送信しています。

+1

ありがとうございました。これらのパラメータを、string.Formatを使用してポストボディに変更しました。その後、元の文字列のURLを "https://accounts.spotify.com/api/token"に変更しました。それを解決すると思っていましたが、今は400エラーです[間違ったリクエスト]。 – Arrivedacci

+1

私は、別のルートを試して、クライアントIDとクライアントシークレットをポストパラメータとして本文に加えて、そのポストパラメータとして追加することにしました。私はこれらの2つをbase64形式でエンコードすることで何が問題になるのかよく分かりませんが、そうであるようです。 – Arrivedacci

関連する問題