api 질문 하나만 드리겠습니다.
본문
<?php
$authurl = "https://oauth.battle.net/token";
$client_id = "42833d8**********************";
$client_secret = "B3766h************************";
// Creating base 64 encoded authkey
$Auth_Key = $client_id.":".$client_secret;
$encoded_Auth_Key=base64_encode($Auth_Key);
$headers = array();
$headers['Authorization'] = "Bearer ".$encoded_Auth_Key;
$headers['Content-Type'] = "application/x-www-form-urlencoded";
//$data = "grant_type=password&scope=read write&username=".$api."&password=".$api."";
$data = "grant_type=client_credentials";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $authurl);
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
$auth = curl_exec( $ch );
if ( curl_errno( $ch ) ){
echo 'Error: ' . curl_error( $ch );
}
curl_close($ch);
$secret = json_decode($auth);
$access_key = $secret->access_token;
echo $access_key;
?>
토큰값을 실시간 불러오게 api를 만들었는데요
$authurl $client_id $client_secret
값은 모두 정상입니다. 포스트맨에서 확인했더니 정상으로 불러옵니다.
(아래 포스트맨에서 테스트 한 내용 - 토큰값 정상으로 나옴)
php로 테스트햇는데 불러오지를 않아서.. 문의드립니다.
틀린 부분이 어디 일가요?
에러도 안납니다. ㅠㅠ
!-->
답변 1
https://develop.battle.net/documentation/guides/using-oauth/client-credentials-flow
Authorization 헤더를 전송하는 스텝은
https://oauth.battle.net/token 을 요청한 후
응답으로 나오는 JSON 값 중 access_token 항목으로 다른 요청에 사용해야 되는것 같고
https://oauth.battle.net/token 에서는
curl -u {client_id}:{client_secret} ...
// -u, --user <user:password> Server user and password ...
유저 계정 옵션이 필요하므로
헤더로 보내는 Authorization 는 부분을 제외하고
계정정보 옵션을 보내주는 구문이 필요한것 같습니다.
...
// $headers['Authorization'] = "Bearer ".$encoded_Auth_Key;
...
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, $client_id.":".$client_secret);
...
답변을 작성하시기 전에 로그인 해주세요.