Google api client->authenticate doesn't work

馋奶兔 提交于 2019-12-24 01:17:08

问题


I try to use google youtube api. Everything is OK. But with $_GET["code"] i cannot get access token. Relative code snippet :

require ('XXX/vendor/autoload.php');
                    $client = new Google_Client();
                    $client->setApplicationName("XXX");
                    $client->setAuthConfig('XXX/client_secrets.json');
                    $client>addScope("https://www.googleapis.com/auth/youtube.readonly");
                    $redirect_uri = 'XXXX';
                    $client->setRedirectUri($redirect_uri);
                    $auth_url = $client->createAuthUrl();
                    if (isset($_GET['code'])) {
                            $test = $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>';

I can write $_GET["code"] in file. But authenticate doesn't work properly. I read EVERY document about it. How can i overcome it ?? My redirect page is this page.


回答1:


You are not going to the auth URL that is being created. try using this after u created ur auth URL

header('Location:'.$auth_url);

Then you should be redirected to google's auth site where the user authenticates himself. After that you put this piece of code on your redirect page

if (isset($_GET['code'])) {
      $test = $client->authenticate($_GET['code']);

}

Also, make sure your client id and secret + redirect URL are set correct.



来源:https://stackoverflow.com/questions/40783404/google-api-client-authenticate-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!