问题描述
我尝试使用google youtube api。一切都好。但是用$ _GET [code]我无法获得访问令牌。相对代码片段:
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-3style =padding-top:10px;>
< a href ='。$ auth_url。'style =border-radius:5px; padding-left:inherit; padding-top:5px; padding-bottom:5px; width:127px; display:块; class =yutub>< i class =fa fa-youtubearia-hidden =true>< / i>
< span class =social-text> YouTube< / span>< i class =fa fa-linkaria-hidden =truestyle =padding-left:15px;> ;< / I>< / A>< / DIV>';
我可以在文件中写入$ _GET [code]。但是,身份验证无法正常工作。我读了关于它的每一个文件。我如何克服它?我的重定向页面就是这个页面。
你不会去创建的auth URL。尝试使用这个后,你创建了我们的认证网址
header('Location:'。$ auth_url);
然后您应该被重定向到用户验证自己的谷歌验证网站。之后,你把这段代码放在你的重定向页面上
pre $
if(isset($ _ GET ['code'])){
$ test = $ client-> authenticate($ _ GET ['code']);
另外,请确保您的客户端ID和密码+重定向网址设置正确。
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.
解决方案 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.
这篇关于Google API客户端>身份验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!