因此,我遵循了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

10-07 19:48
查看更多