OAuth2-请求访问令牌-cURL

OAuth2-请求访问令牌-cURL,curl,Curl,经过几个小时的尝试,我无法让它工作。我已经尝试过其他方法,但它们似乎也不起作用。最后我改为cURL,因为他们想要一个URL编码的主体。也没有像预期的那样起作用 我要做的是请求获取访问令牌 POST https://api.twitch.tv/kraken/oauth2/token 帖子正文(URL编码): 到目前为止,我的代码是: <?php // cURL resource $curl = curl_init(); // call code $usercode = $_GET['c

经过几个小时的尝试,我无法让它工作。我已经尝试过其他方法,但它们似乎也不起作用。最后我改为cURL,因为他们想要一个URL编码的主体。也没有像预期的那样起作用

我要做的是请求获取访问令牌

POST https://api.twitch.tv/kraken/oauth2/token
帖子正文(URL编码):

到目前为止,我的代码是:

<?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);
?>

希望你能解释一下我做错了什么,以便学习。 提前感谢您的帮助。

根据

“绝大多数情况下,我们不提供密码授予。您需要明确获得twitch的许可才能使用密码凭据流。如果没有我们的明确许可,它将失败并出现错误。”


您必须使用JavaScript API而不是RESTful API。

您得到了什么响应?此外,您可能需要将
客户机id
客户机机密
清空。
<?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);
?>