2016-07-04 15 views
1

私はちょうどangularjsの学習を始めました。私はapiからトークンを取得しようとしています。ターミナルで使用するときにカールコマンドがうまく動作し、トークンを正常に取得できます。私は$ httpを使って動作させるのに苦労しています。ここで

は私のcurlコマンドです:

curl --data "grant_type=authorization_code&client_id=CLIENT_ID&client_secret=SECRET_KEY&redirect_uri=http://localhost:9999&code=AUTH_CODE" https://www.twitchalerts.com/api/v1.0/token 

誰も私が$ HTTP

+1

'$ http'コードはどこで試されましたか?あなたのエラーハンドラはあなたに何を伝えますか?要求を行おうとするとブラウザコンソールに何が表示されますか? – charlietfl

+0

クライアントの秘密鍵が入っているということは、おそらくブラウザであなたの資格情報を公開することを意味するので、おそらくajaxでこれを行うことができないことを示唆しています。 – charlietfl

答えて

1

を使用して、これを変換することができ、このお試しください:この

.... 
data:data, 
headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
.... 
にしようとした後でない場合

var data = { 
    grant_type  :'authorization_code', 
    client_id  :'CLIENT_ID', 
    client_secret :'SECRET_KEY', 
    redirect_uri :'http://localhost:9999&code=AUTH_CODE' 
}; 
// Simple GET request example: 
$http({ 
    method: 'GET',//or POST 
    url: 'https://www.twitchalerts.com/api/v1.0/token', 
    data:data, 
}).then(function successCallback(response) { 
    // this callback will be called asynchronously 
    // when the response is available 
}, function errorCallback(response) { 
    // called asynchronously if an error occurs 
    // or server returns response with an error status. 
}); 

詳細about $ http https://docs.angularjs.org/api/ng/service/ $ http

+0

'data'と' params'はGETとPOSTでは互換性がありません。 contentTypeがありません – charlietfl

+0

@charlietfl whithメソッドGET、enctype = "application/x-www-form-urlencoded | multipart/form-data | text/plain"を送信すると、どのようなヘッダが送信されるか? :) –

+0

まあ、GETには要求本体がないので、Content Typeは必要ありません。すべてのデータはURLにあります – charlietfl

関連する問題