api 질문 하나만 드리겠습니다.

api 질문 하나만 드리겠습니다.

QA

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 

값은 모두 정상입니다. 포스트맨에서 확인했더니 정상으로 불러옵니다.

(아래 포스트맨에서 테스트 한 내용 - 토큰값 정상으로 나옴)

238304609_1693236166.6838.png

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

<?php
//$api = "KEY GOES HERE";
$authurl = "https://oauth.battle.net/token";

$client_id = "42833d8260cb470499cce604d7da5e6d";
$client_secret = "B3766hsusgvwrMo62kfcr1F1luQggP32";

// 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_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, $client_id.":".$client_secret);
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;
?>

선생님 이렇게 맞을가요? ㅠㅠ


...
//$data = "grant_type=client_credentials";
$data = ['grant_type' => 'client_credentials'];

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $client_id.":".$client_secret);
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);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
...

답변을 작성하시기 전에 로그인 해주세요.
전체 54
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT