用php和google library提交表单后在Google电子表格上书写

时间:2018-08-11 05:04:19

标签: php google-api-php-client

我要用php提交表单后更新Google电子表格

我使用了“ google-api-php-client”库

我的代码下面是使用OAuth2身份验证进行身份验证

private function authenticate()
    {
        /*
         * Authentication using the library 'google-api-php-client' which uses 'Oauth2'.
         */
        require 'google-api-php-client/src/Google/autoload.php';
        $scopes = array('https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive');
        $pri_key = file_get_contents(realpath(dirname(__FILE__)).'/My Project service account.p12');
        $credentials = new Google_Auth_AssertionCredentials(
           'CLIENT_ID', #google dev console email
           $scopes,
           $pri_key
        );

        $this->client = new Google_Client();
        $this->client->setAssertionCredentials($credentials);
        if ($this->client->getAuth()->isAccessTokenExpired()) {
            $this->client->getAuth()->refreshTokenWithAssertion();
        }

        $tokenData = json_decode($this->client->getAccessToken());
        $this->accessToken = $tokenData->access_token;
    }

在这里,我已在自己的gmail帐户中创建了一个服务帐户和一个Web应用程序,并且从那里开始,我使用了客户端ID和p12文件,但这给了我下面的错误提示,

Google_Auth_Exception
IT WAS THROWN IN
\google-api-php-client\src\Google\Auth\OAuth2.php on line 363

MESSAGE
Error refreshing the OAuth2 token, message: '{
"error" : "invalid_grant",
"error_description" : "Invalid JWT Signature."
}'

我在这方面有什么遗漏吗?还是遵循错误的流程?

0 个答案:

没有答案