2016-11-24 2 views
0

Google youtube api를 사용하려고합니다. 다 잘 됐어. 문서와 마찬가지로 클라이언트 객체를 형성합니다. 나는 YouTube에 리디렉션하여 권한을 부여하고 허락합니다. 그런 다음 $_GET["code"]을 가져갑니다. 하지만 액세스 토큰으로 교환 할 수 없습니다. client -> authenticate 메서드에서 실행이 중지되었습니다. 상대 코드 스 니펫 :

<?php 

    require_once ('XXX/vendor/autoload.php'); 
    $client = new Google_Client(); 
    $client->setApplicationName("AdKeek"); 
    $client->setAuthConfig('XXX/client_secrets.json'); 
    $client->addScope("https://www.googleapis.com/auth/youtube.readonly"); 
    $redirect_uri = 'XXX THIS PAGE'; 
    $client->setRedirectUri($redirect_uri); 
    $auth_url = $client->createAuthUrl(); 
    if (isset($_GET['code'])) { 
      $client->authenticate($_GET['code']); 
    } 
    echo '<div class="col-xs-6 col-sm-3" style="padding-top: 10px;"> 
      <a href="'.$auth_url.'" style="border-radius: 5px;padding-left: inherit;padding-top: 5px;padding-bottom: 5px;width: 127px; display: block;" class="yutub" ><i class="fa fa-youtube" aria-hidden="true"></i> 
      <span class="social-text">YouTube</span><i class="fa fa-link" aria-hidden="true" style="padding-left: 15px;"></i></a></div>'; 

?> 

거의 모든 문서를 읽었지만 문제는 찾을 수 없습니다. 문제가 무엇입니까 ???

편집 = 내 localhost에서 시도해보십시오. 그리고이 오류 = GuzzleHttp \ Exception \ RequestException : cURL 오류가 발생합니다.

+0

'$ _GET [ 'code']'에 값이 있습니까? –

+0

예. 파일에 썼습니다. 정확한 내용입니까? – CaveMan

+0

? 나는 버튼을 형성하고 HTML 코드에서 auth_url을 제공한다. – CaveMan

답변

0

나는 인증 흐름을 적절히 따르지 않는다고 생각합니다. $auth_url = $client->createAuthUrl(); 이후에 인증을 받으려면 리디렉션되어야하며 액세스 코드를 받게됩니다. header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));이 코드를 대신 사용해보십시오.

<?php 

require_once ('XXX/vendor/autoload.php'); 
$client = new Google_Client(); 
$client->setApplicationName("AdKeek"); 
$client->setAuthConfig('XXX/client_secrets.json'); 
$client->addScope("https://www.googleapis.com/auth/youtube.readonly"); 
$redirect_uri = 'XXX THIS PAGE'; 
$client->setRedirectUri($redirect_uri); 

if (!isset($_GET['code'])) { 

    $auth_url = $client->createAuthUrl(); 

    echo '<div class="col-xs-6 col-sm-3" style="padding-top: 10px;"> 
     <a href="'.$auth_url.'" style="border-radius: 5px;padding-left: inherit;padding-top: 5px;padding-bottom: 5px;width: 127px; display: block;" class="yutub" ><i class="fa fa-youtube" aria-hidden="true"></i> 
     <span class="social-text">YouTube</span><i class="fa fa-link" aria-hidden="true" style="padding-left: 15px;"></i></a></div>'; 

} else { 

    $client->authenticate($_GET['code']); 
    //whatever else you want to do 

} 


?> 

이제 액세스 코드를 저장 한 경우 인증 URL을 만들어야합니다. 대신 파일에서 액세스 코드를 가져온 다음 $client->authenticate($code);을 사용하십시오. 여기서 $ code = file의 값입니다.

+0

이 문서를 참조하십시오. 작동하지 않습니다. – CaveMan