如何使用PHP在Google Spreadsheet Authentication中使用JSON密钥代替P12? - php

我正在使用PHP中的Google Spreadsheet。当我使用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;
    }

PHP getallheaders替代 - php

我正在尝试从服务器上的apache切换到nginx。唯一的问题是我在PHP脚本中使用的getallheaders()函数,该函数不适用于Nginx。我已经尝试过用户在getallheaders函数上的php站点上提供的注释,但这并不返回所有请求标头。请告诉我如何解决这个问题。我真的想切换到Nginx。 参考方案 您仍然可以使用它,但是您必须像这里一样重新定义…

php Singleton类实例将在多个会话中保留吗? - php

举一个简单的例子,如果我想计算一个不使用磁盘存储的脚本的命中次数,我可以使用静态类成员来执行此操作吗?用户1:<?php $test = Example::singleton(); $test->visits++; ?> 用户2:<?php $test = Example::singleton(); $test->visits+…

PHP:将字符串拆分为字母和数字部分的最佳方法 - php

我有几个格式的字符串AA11 AAAAAA1111111 AA1111111 分离字符串的字母和数字部分的最佳方法(最有效)? 参考方案 如果它们都是一系列字母,然后是一系列数字,并且没有非字母数字字符,那么sscanf()可能比regexp更有效$example = 'AAA11111'; list($alpha,$numeric) =…

php-casperjs获取内部文本 - php

我正在为casperjs使用php包装器-https://github.com/alwex/php-casperjs我正在网上自动化一些重复的工作,我需要访问一个项目的innerText,但是我尚不清楚如何从casperjs浏览器访问dom。我认为在js中我会var arr = document.querySelector('label.input…

PHP strtotime困境 - php

有人可以解释为什么这在我的服务器上输出为true吗?date_default_timezone_set('Europe/Bucharest'); var_dump( strtotime('29.03.2015 03:00', time()) === strtotime('29.03.2015 04:00�…