当前位置:  开发笔记 > 编程语言 > 正文

Google Client API - 缺少require参数:redirect_uri

如何解决《GoogleClientAPI-缺少require参数:redirect_uri》经验,为你挑选了2个好方法。

所以我按照快速入门指南决定将其分解为一个名为scheduler的类.我正在处理身份验证代码,但我一直得到这个:"错误400(OAuth 2错误)错误无效请求缺少所需参数:redirect_uri".

class scheduler{

//The Google Client object
private $googleClient;

//the Google Calendar Service ojbect
private $calendarService;

/*
*   Google Calendar Setup
*
*   This creates a Google Client object so that you may create a Google Calendar object.
*
*/
function __construct(){
    //set the application name
    define("APPLICATION_NAME", "Web client 1");
    //
    define("CREDENTIALS_PATH", "~/scheduler/credentials.json");
    //
    define("CLIENT_SECRET_PATH", __DIR__ . "/scheduler/client_secret.json");
    //
    define("SCOPES", implode(" ", array(Google_Service_Calendar::CALENDAR_READONLY)));

    /*if(php_sapi_name() != "cli"){
        throw new Exception("This application must be run on the command line");    
    }*/

    //create the google client
    $this->googleClient = new Google_Client();

    //setup the client
    $this->googleClient->setApplicationName(APPLICATION_NAME);
    $this->googleClient->setDeveloperKey("AIzaSyBmJLvNdMYuFhVpWalkUdyStrEBoVEayYM");
    $this->googleClient->setScopes(SCOPES);
    $this->googleClient->setAuthConfigFile(CLIENT_SECRET_PATH);
    $this->googleClient->setAccessType("offline");

    //get the credentials file path
    $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);

    //if the file exists
    if(file_exists($credentialsPath)){

        //get the credentials from the file
        $accessToken = file_get_contents($credentialsPath); 

    }//if it does not
    else{

        //request the authorization url
        $authURL = $this->googleClient->createAuthUrl();
        //print the authorization ulr
        echo "Press Me

"; //prompt the user to enter the auth code print("Enter authentication code: "); // $authCode = trim(fgets(STDIN)); //exchange authorization for an access token $accessToken = $this->googleClient->authenticate($authCode); //store credentials to disk if(!file_exists(dirname($credentialsPath))){ mkdir(dirname($credentialsPath), 0700, true); } //put the contents into the credential files file_put_contents($credentialsPath, $accessToken); } $this->googleClient->setAccessToken($accessToken); //refresh token if its expired if($this->googleClient->isAccessTokenExpired()){ $this->googleClient->refreshToken($client->getRefreshToken()); file_put_contents($credentialsPath, $this->googleClient->getAccessToken()); } }

我发现了问题的原因,没有解决方案.在我的Google Developer Console下,我尝试将" http:// localhost / "放入"授权重定向URI"部分.它给了我这个错误"抱歉,有问题.如果您输入了信息,请检查并重试.否则,问题可能会自行解决,请稍后再回来查看." 有没有办法让Google Developer Console接受本地主机服务器的重定向uri?



1> James07..:

只需setRedirectUri($absoluteUrl)在客户端对象上使用方法:

$client = new Google_Client();
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php');

通过:

https://developers.google.com/api-client-library/php/auth/web-app



2> Joshua Blevi..:

我得到了它的工作.我要做的就是回到Google Developer Console并删除我创建的项目.然后在制作一个新项目时,它允许我保存我的localhost网址.发生的问题是当我去添加我的localhost网址到重定向网址时,它会说这不可能.当我在点击创建按钮之前设置重定向网址时,它接受它就好了.


这是因为你下载的客户端秘密json文件也需要更新.因此,如果您在没有uri的情况下创建凭据并下载json,则json没有'redirect_uris'属性.如果您然后将其添加到仪表板中,则需要重新下载客户端密钥json并将其添加到项目中.
推荐阅读
夏晶阳--艺术
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有