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

时间:2015-11-05 05:56:39

标签: php json google-sheets

我正在使用PHP中的Google Spreadsheet。当我使用P12键时,它工作得很完美,但是当我使用Google Spread Sheet进行身份验证时使用JSON密钥而不是P12密钥时,它正在提供

  

致命错误:未捕获的异常' Google_Auth_Exception'有消息'无法加载私钥'

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

2 个答案:

答案 0 :(得分:4)

这是我找到的解决方案。

对于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 :(得分:1)

访问令牌只有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();