如何在使用PHP的Google电子表格身份验证中使用JSON密钥而不是P12


How to use JSON key instead of P12 in Google Spreadsheet Authentication with PHP?

我正在使用PHP中的Google电子表格。当我使用P12密钥时,它非常有效,但当我在使用Google Spread Sheet进行身份验证时使用JSON密钥而不是P12密钥,它会给出

致命错误:未捕获异常"Google_Auth_exception",消息为"无法加载私钥"

请告诉我如何在PHP中使用Google Spread Sheet进行身份验证时使用JSON密钥。

这是我找到的解决方案。

对于P12键
通常,我们使用以下代码来生成令牌。

public static function getToken()
    {
        $key = file_get_contents( APPLICATION_DIR.'/'.APPLICATION_GOOGLE_CLIENT_KEY_FILE);
        $cred = new Google_Auth_AssertionCredentials(
            APPLICATION_GOOGLE_CLIENT_EMAIL,
            array('https://spreadsheets.google.com/feeds'),
            $key
        );
        $client = new Google_Client();
        $client->setAssertionCredentials($cred);
        if (!$client->getAuth()->isAccessTokenExpired()) {
            return false;
        }
        else {
            $client->getAuth()->refreshTokenWithAssertion($cred);
        }
        $service_token = json_decode($client->getAccessToken());

        return $service_token->access_token;
    }

对于JSON密钥
但现在我们没有P12密钥,我们有一个JSON密钥,所以我只是在上面的代码中做了一些更改,请看下面:

public static function getToken()
    {
        $key = APPLICATION_DIR.'/'.APPLICATION_GOOGLE_CLIENT_KEY_FILE;
        $data = json_decode(file_get_contents($key));    // here i decoded the json
         if (isset($data->type) && $data->type == 'service_account') {
            $cred = new Google_Auth_AssertionCredentials(
                APPLICATION_GOOGLE_CLIENT_EMAIL,    // it's the client email
                array('https://spreadsheets.google.com/feeds'),   // it's google spreadsheet scope
                $data->private_key         // here is the private key
            );
         }
        $client = new Google_Client();
        $client->setAssertionCredentials($cred);
        if (!$client->getAuth()->isAccessTokenExpired()) {
            return false;
        }
        else {
            $client->getAuth()->refreshTokenWithAssertion($cred);
        }
        $service_token = json_decode($client->getAccessToken());

        return $service_token->access_token;
    }

访问令牌仅在1小时内有效,并以解码后的json形式发送。所以你可能必须先解码它。

$token = json_decode($client->getAccessToken());
$serviceRequest = new DefaultServiceRequest($token->access_token);
ServiceRequestFactory::setInstance($serviceRequest);

使用p12键应该可以正常工作。

session_start();
define('GOOGLE_CLIENT_ID','');
define('GOOGLE_CLIENT_EMAIL','');
define('GOOGLE_SPREADSHEETS_SCOPE','https://spreadsheets.google.com/feeds');
define('GOOGLE_APPLICATION_NAME','whatever');
define('GOOGLE_KEY_FILE','xxxxxxxxxxxxxx.p12'); // pass for key: notasecret
function getToken()
{
    $client = new Google_Client();
    $client->setApplicationName(GOOGLE_APPLICATION_NAME);
    $client->setClientId(GOOGLE_CLIENT_ID);
    $key = file_get_contents(GOOGLE_KEY_FILE);
    $cred = new Google_Auth_AssertionCredentials(
        GOOGLE_CLIENT_EMAIL,
        array(GOOGLE_SPREADSHEETS_SCOPE),
        $key
    );
    $client->setAssertionCredentials($cred);
    if($client->getAuth()->isAccessTokenExpired()) {
        $client->getAuth()->refreshTokenWithAssertion($cred);
    }
    $service_token = json_decode($client->getAccessToken());
    return $service_token->access_token;
}
require("vendor/autoload.php");
use Google'Spreadsheet'DefaultServiceRequest;
use Google'Spreadsheet'ServiceRequestFactory;
$_SESSION['access_token']=getToken();
$serviceRequest = new Google'Spreadsheet'DefaultServiceRequest($_SESSION['access_token']);
Google'Spreadsheet'ServiceRequestFactory::setInstance($serviceRequest);
$spreadsheetService = new Google'Spreadsheet'SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheets();