PHP   发布时间:2019-11-21  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php – Google Client API – 缺少require参数:redirect_uri大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我按照 quickstart指南决定将其分解为一个名为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 "<a href=\"$authURL\">Press Me</a><br /><br />";

        //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?

解决方法

只需在客户端对象上使用方法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

大佬总结

以上是大佬教程为你收集整理的php – Google Client API – 缺少require参数:redirect_uri全部内容,希望文章能够帮你解决php – Google Client API – 缺少require参数:redirect_uri所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。