안녕하십니까? 저는 도움을 청하기 좋아하는 사람이 아니지만,이 경우 IMO가 내 문제를 해결할 수있는 유일한 방법입니다. Google은별로 도움이되지 못했습니다.트 위치 API - PHP를 사용하여 인증 토큰을 가져올 수 없습니다.
So. 내 문제 : Twitch API를 사용하여 일부 데이터를 가져 오려고합니다. 쉬운 소리? 나는 그것이 좋겠다고 생각한다. 내 실제 코드를 게시하도록하겠습니다 아래 (... 그것은 작은 없습니다하지만 시대의 다양한 수정 된 지금은 같은 모양) :
$user = json_decode(file_get_contents('https://api.twitch.tv/kraken/oauth2/authorize?response_type=code&client_id=MY_CORRECT_CLIENT_ID&redirect_uri=http://localhost/php/twitch.php&scope=user_read'), true);
print_r($user); // returns nothing
$token = $user['access_token'];
print_r($token); // same as above
$ch = curl_init();
// some stupid curls
curl_setopt($ch, CURLOPT_URL, 'https://api.twitch.tv/kraken/streams/followed');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Authorization: OAuth '.$token);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$retval = curl_exec($ch);
curl_close($ch);
$result = json_decode($retval, true);
그것은 반환 ... 아무것도. 그래서 discussion.twitch에서 준비된 솔루션을 사용했습니다. (나는이 코드의 저자의 이름을 쓸 수 싶지만 나는 다시 검색 너무 피곤 해요 어느 쪽이든 감사합니다.!) :
$ch = curl_init("https://api.twitch.tv/kraken/oauth2/token");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$fields = array(
'client_id' => 'blablabla_correct',
'client_secret' => 'blablabla_also_correct',
'grant_type' => 'authorization_code',
'redirect_uri' => 'http://localhost/php/twitch.php',
'code' => $_GET['code']
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$data = curl_exec($ch);
$response = json_decode($data, true);
//var_dump($response);
$access_token = $response["access_token"];
echo $access_token;
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$returnobj = curl_exec($ch);
curl_close($ch);
return $returnobj;
}
$testobj = json_decode(get_data("https://api.twitch.tv/kraken/user?oauth_token=".$access_token."&client_id=".$fields['client_id']));
echo "<br>Data: ";
print_r($testobj);
위의이 코드는 조금 낫다. 단지 약간. 오류 401을 반환합니다. 왜? 인증 토큰을 얻을 수 없으므로 글쎄, 그건 내가 얻고 싶었던 것이 아닌 무언가이다. 지금 어떻게해야합니까? 로컬 호스트 주소가 잘못되었을 수도 있습니다.
FAQ (?) : 예, 내 트 위치 응용 프로그램 설정 페이지의 올바른 데이터를 사용하고 있습니다. 예, 혼란스러워합니다.
이 첫 번째 디버깅 단계 후에 자세한 정보를 제공 할 수 있다면 다른 단계를 시도해 볼 수 있습니다. – Jason
redirect_uri는 API 문서에 있으며이 요청에 포함되어야합니다. 코드가 제대로 작동하고 문제가 해결되었습니다. 고맙습니다. – user3654084