我正在開發一個項目,以幫助我學習如何使用curl
到PHP
。我試圖使用我自己的帳戶從Twitch-API
獲取數據進行測試。在PHP中使用捲曲
我已經成功地通過身份驗證自己的帳戶與我的域名:
https://api.twitch.tv/kraken/oauth2/authorize?response_type=code&client_id=...&redirect_uri=...&scope=user_read+channel_read+channel_subscriptions+user_subscriptions+channel_check_subscription&state=...
我已刪除client_id
,redirect_uri
和state
顯示我使用的鏈接。
一旦成功通過身份驗證,它將返回到我指定的域(redirect_uri
),一旦它返回到該域,網站只會知道一旦用戶接受了來自twitch的生成的身份驗證密鑰。
例AUTH:3ofbaoidzkym72ntjua1gmrr66o0nd
現在我想能夠得到用戶的用戶名,there is documentation on it:
curl -H 'Accept: application/vnd.twitchtv.v3+json' -H 'Authorization: OAuth <access_token>' \
-X GET https://api.twitch.tv/kraken/user
我試圖做到這一點在PHP,但我不瞭解捲曲功能......這裏是我到目前爲止有:
<?php if(isset($_GET['code']) && isset($_GET['scope'])) { ?>
<pre>
<?php
$auth = $_GET['code'];
$twitch = curl_init();
$headers = array();
$headers[] = 'Accept: application/vnd.twitchtv.v3+json';
$headers[] = 'Authorization: OAuth ' .$auth;
curl_setopt($twitch, CURLOPT_HEADER, $headers);
curl_setopt($twitch, CURLOPT_URL, "https://api.twitch.tv/kraken/user");
curl_exec($twitch);
?>
</pre>
<?php }; ?>
當我嘗試運行這段代碼,我得到了一些錯誤:
HTTP/1.1 401 Unauthorized
Server: nginx
Date: Sat, 08 Aug 2015 13:43:51 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 89
Connection: keep-alive
Status: 401 Unauthorized
X-API-Version: 3
WWW-Authenticate: OAuth realm='TwitchTV'
Cache-Control: max-age=0, private, must-revalidate
Vary: Accept-Encoding
X-UA-Compatible: IE=Edge,chrome=1
X-Request-Id: 4bc2e0bfadf6817366b4eb19ab5751bf
X-Runtime: 0.007862
Accept-Ranges: bytes
X-Varnish: 1641121794
Age: 0
Via: 1.1 varnish
X-MH-Cache: rails-varnish-5cb970; M
{"error":"Unauthorized","status":401,"message":"Token invalid or missing required scope"}
但我對如何作爲,對我來說,似乎我/所做的一切,該文件說,這樣做解決這個問題不清楚...
我應該如何去修復這個問題?
編輯:
看來,如果我要求使用我twitch username工作:
curl -H 'Accept: application/vnd.twitchtv.v3+json' \
-X GET https://api.twitch.tv/kraken/users/test_user1
我使用的用戶名代碼:
<?php
$auth = urlencode($_GET['code']);
$twitch = curl_init();
$headers = array();
$headers[] = 'Accept: application/vnd.twitchtv.v3+json';
#$headers[] = 'Authorization: OAuth ' .$auth;
curl_setopt($twitch, CURLOPT_HTTPHEADER , $headers);
curl_setopt($twitch, CURLOPT_URL, "https://api.twitch.tv/kraken/users/...");
curl_exec($twitch);
?>
但我不知道用戶的用戶名,除非我從產生錯誤的語句中獲取它並將其存儲在數據庫中。
編輯:
讀入文件升技更多的,它需要的範圍以及訪問令牌。我已經能夠得到這樣的:
例子:
Array
(
[0] => Accept: application/vnd.twitchtv.v3+json
[1] => Authorization: OAuth code=scn89zerug002sr6r95z9ngbxmd0d2&scope=user_read+channel_read+channel_subscriptions+user_subscriptions+channel_check_subscription
)
但我仍然得到錯誤...
編輯:
所以我通過文檔更,現在我已經得到了這個閱讀:
class twitch {
var $base_url = "https://api.twitch.tv/kraken/";
var $client_id = "...";
var $client_secret = "...";
var $return_url = "...";
var $scope_array = array('user_read','channel_read','channel_subscriptions','user_subscriptions','channel_check_subscription');
public function get_access_token($code,$state) {
$ch = curl_init($this->base_url . "oauth2/token");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$fields = array(
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirect_url,
'code' => $code
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$data = curl_exec($ch);
$response = json_decode($data, true);
curl_close($ch);
echo "<pre>".print_r($this->redirect_url,true)."</pre>";
echo "<pre>".print_r($response,true)."</pre>";
return $response["access_token"];
}
};
$auth = new twitch();
print_r($auth->get_access_token($_GET['code'],$_GET['state']));
但這一次是另一個錯誤,說我'redirect_uri' => $this->redirect_url
與抽搐持有的不同。
Array
(
[error] => Bad Request
[status] => 400
[message] => Parameter redirect_uri does not match registered URI
)
我甚至已經複製和抽搐的網站,我的變量和周圍的其他方式粘貼,我仍然得到同樣的錯誤......現在我更堅持,但至少近了一步。
您需要'CURLOPT_HTTPHEADER'來設置您的自定義Authorization:請求標頭。 (比喻'CURLOPT_HEADER'主要用於調試。) – mario
@mario,嘿!感謝評論,'CURLOPT_HTTPHEADER'隱藏調試,它仍然不能解決我的問題。但是,謝謝!當我向前移動時,我會記住這一點:) – Comms
我想你想''.urlencode($ auth);' – Mihai