2015-08-08 127 views
3

我正在開發一個項目,以幫助我學習如何使用curlPHP。我試圖使用我自己的帳戶從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_idredirect_uristate顯示我使用的鏈接。

一旦成功通過身份驗證,它將返回到我指定的域(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 
) 

我甚至已經複製和抽搐的網站,我的變量和周圍的其他方式粘貼,我仍然得到同樣的錯誤......現在我更堅持,但至少近了一步。

+0

您需要'CURLOPT_HTTPHEADER'來設置您的自定義Authorization:請求標頭。 (比喻'CURLOPT_HEADER'主要用於調試。) – mario

+0

@mario,嘿!感謝評論,'CURLOPT_HTTPHEADER'隱藏調試,它仍然不能解決我的問題。但是,謝謝!當我向前移動時,我會記住這一點:) – Comms

+0

我想你想''.urlencode($ auth);' – Mihai

回答

1

正確的我會和你一起做這件事,因爲我這樣做:d到目前爲止,我已經能夠得到一個用戶,你得到錯誤的原因是因爲你沒有設置任何捲曲選項。我教了自己使用這個https://github.com/paypal/rest-api-curlsamples/blob/master/execute_all_calls.php,我發現大人物在學習捲曲時很有幫助。代碼本身是基本的,但它很容易閱讀。我設法瞭解它,並使其100%更復雜:D

首先,我會告訴你我是如何得到測試用戶的。 你想要做的是設置選項,我會先保持簡單的方法。

2種方法是CURLOPT_HEADERCURL_RETURNTRANSFER。您可以使用init函數設置您的url。

$twitch=curl_init('https://api.twitch.tv/kraken/users/test_user1'); 
curl_setopt($twitch,CURLOPT_HTTPHEADER,array('Accept: application/vnd.twitchtv.v3+json'));//must be an array. 
curl_setopt($twitch,CURLOPT_RETURNTRANSFER,true); 
$result=curl_exec($twitch); 
$info=curl_getinfo($twitch); 
print_r($result); 

這將讓你的測試用戶,並希望告訴你一些關於你做錯了什麼。如果你想使用數組方法,那麼你必須使用你的curl選項作爲數組鍵,以便set函數知道如何設置。 (不要問我在技術上是如何工作的:S)

我將更新以向您展示如何在我完成工作後獲得授權和數據。但基本原則是你需要發送發佈數據並將CURLOPT_POST設置爲true,並且包含postdata CURLOPT_POSTFIELDS,它必須是一個json數組,因爲你的應用程序需要json我相信嗎?

反正數組:

curl_set_opts($twitch,array(CURLOPT_HEADER=>array('Accept: application/vnd.twitchtv.v3+json',CURLOPT_RETURNTRANSFER=true)); 

看到,因爲你已經知道如何授權我將跳過該位用戶,但我建議你使用的東西一點點比$_GET更安全。也許會話變量會好一點。

獲取使用返回的Auth的特定用戶。你想要做這樣的事情:(對不起,我不能測試它自己,我沒有抽搐dev的帳戶)

$twitch=curl_init('https://api.twitch.tv/kraken/user'); 
curl_setopt($twitch,CURLOPT_HEADER,array('Accept: application/cvd.twitchtv.v3+json','Authorization: OAuth '.$_SESSION['token'])); 
curl_setopt($twitch,CURLOPT_RETURNTRANSFER,true); 
$result=curl_exec($twitch); 
print_r($result); 
//don't forget to close! 
curl_close($twitch); 
$user=json_decode($result); 

echo$user->display_name; 

這應該工作,雖然我不知道你是如何得到一個的OAuth令牌大聲笑

如果你想成爲一個非常酷的程序員8 |我建議爲此做一些類。像這樣

class twitch{ 
    private$token,$twitch,$url="http://api.twitch.tv/kraken/"; 
    protected$code,$state,$report; 
    private static$details; 
    public function __construct($code,$state){ 
     $this->code=$code; 
     $this->state=$state; 
     self::$details=(object)array('client_id'=>'id','client_secret'=>'secret','return_url'=>'redirect'); 
     $result=$this->makeCall('oauth2/token',true); 
     print_r($result); 
    } 
    protected function makeCall($extention,$auth=false,$object=true){ 
     $this->twitch=curl_init($this->url.$extention); 
     //$opts=array(CURLOPT_) 
     if($auth!==false){ 
      $opts=array(CURLOPT_FOLLOWLOCATION=>true,CURLOPT_RETURNTRANSFER=>true,CURLOPT_POST=>true,CURLOPT_POSTFIELDS=>json_encode(array('client_id'=>self::$details->client_id,'client_secret'=>self::$details->client_secret,'grant_type'=>'authorization_code','code'=>$this->code,'redirect_uri'=>self::$details->return_url))); 
     }else{ 
      $opts=array(CURLOPT_HEADER=>array('Accept: application/cvd.twitchtv.v3+json','Authorization: OAuth '.$this->token),CURLOPT_RETURNTRANSFER=>true); 
     } 
     curl_setopt_array($this->twitch,$opts); 
     $result=curl_exec($this->twitch); 
     $this->report=array('info'=>curl_getinfo($this->twitch),'error'=>curl_error($this->twitch)); 
     curl_close($this->twitch); 
     return($object===true)?json_decode($result):$result; 
    } 
    protected function userDetails(){ 
     return$this->makeCall('user'); 
    } 
    public function user(){ 
     return$this->userDetails(); 
    } 
}  
+0

嘿,那裏,謝謝你的迴應,雖然這似乎不是它的工作原理,但它仍然有幫助。謝謝。 – Comms

+0

@hello ahhh好吧,是的,這有點奇怪的設置?你必須去網址得到迴應?然後使用它在重定向網址中提供的OAuth令牌?狂!您可能想要檢查最後兩個代碼示例。我不確定auth是如何工作的,所以最後一個例子可能不起作用,但它會給你一個關於如何構建發送接收數據的好主意。如果你還沒有看到迴應。也許嘗試使用'curl_getinfo()'和'curl_error()'來查看有哪些錯誤。 – sourRaspberri

+0

我已更新我的_question_,盡情享受吧! – Comms