2015-08-08 93 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