我正在做一个项目,以帮助我学习如何通过PHP
使用PHP
。我正在尝试使用我自己的帐户从Twitch-API
获取数据进行测试。
我已经成功地使用我的域验证了我的帐户,方法是:
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
),一旦它返回到该域,网站只知道用户接受后从抽搐中生成的身份验证密钥。
示例auth:3ofbaoidzkym72ntjua1gmrr66o0nd
现在我希望能够获得用户的用户名,上面有文件
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"}
但是我不确定如何解决这个问题,因为,在我看来,我已经做了所有文件上说要做的事情.
我该如何解决这个问题呢?
编辑:
如果我请求使用我的抽动用户名,这似乎是可行的。
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
与通过抽搐持有的'redirect_uri' => $this->redirect_url
不同。
Array
(
[error] => Bad Request
[status] => 400
[message] => Parameter redirect_uri does not match registered URI
)
我甚至复制和粘贴从抽搐网站到我的变量和反过来,我仍然得到同样的错误.现在我被困得更深了,但至少更近了一步。
发布于 2015-08-08 07:53:28
是的,我要和你一起做这个,就像我做的那样:d到目前为止,我已经有了一个用户,你得到错误的原因是你没有设置任何卷曲选项。我是用这个calls.php自学的,在学习卷发的时候我发现这是非常有帮助的。代码本身是基本的,但它很容易阅读。我设法理解了它,使它更加复杂:D
首先,我将向您展示如何获得测试用户。您要做的是设置选项,我将首先使用简单的方法。
这两种方法分别是CURLOPT_HEADER
和CURL_RETURNTRANSFER
。您的url可以用init函数设置。
$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)
我将更新,以向您展示如何获得授权和数据,一旦我已经解决了。但是,基本原则是您需要发送post数据并将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获取特定用户。你想做这样的事情:(对不起,我不能亲自测试,我没有twitch帐户)
$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令牌lol的,但这应该是可行的
如果你想成为一个真正酷酷的程序员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();
}
}
https://stackoverflow.com/questions/31893829
复制