Google 客户端 API - 缺少要求参数:redirect_uri

新手上路,请多包涵

所以我遵循了 快速入门 指南并决定将其分解为一个名为 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?

原文由 Joshua Blevins 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 654
2 个回答

我让它工作了。我必须做的是返回到 Google Developer Console 并删除我创建的项目。然后在制作一个新项目时,它允许我保存我的本地主机 url。发生的问题是当我去将我的本地主机 url 添加到重定向 url 时,它会说目前不可能。当我在点击创建按钮之前设置重定向 url 时,它接受它就好了。

原文由 Joshua Blevins 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏