经过几个小时的尝试,我无法让它工作。我已经尝试过其他方法,但它们似乎也不起作用。我最后改成了 cURL,因为他们想要一个 URL 编码的正文。也没有按预期工作。
我要做的是发出请求以获取访问令牌。
POST https://api.twitch.tv/kraken/oauth2/token
帖子正文(URL 编码):
client_id=[your client ID]
&client_secret=[your client secret]
&grant_type=authorization_code
&redirect_uri=[your registered redirect URI]
&code=[code received from redirect URI]
到目前为止我的代码:
<?php
// cURL resource
$curl = curl_init();
// call code
$usercode = $_GET['code'];
// Set some options - passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.twitch.tv/kraken/oauth2/token',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
usercode => $usercode,
body => 'client_id=91th8b4i2ouxdfn660xojhu1eqfaonu&client_secret=dqhzi4h2k2rnb67bzbno9kva43uwmf9&grant_type=authorization_code&redirect_uri=http://decimo.net/token.php&code=', $usercode
)
));
// Send request and save response to $response
$response = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
?>
希望你能解释我做错了什么,以便学习。提前感谢您帮助我。