我正在开发一个项目,以帮助我学习如何使用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