Google服务帐户身份验证PHP

我正在尝试对服务帐户进行身份验证,以便我可以将访问令牌与客户端JSON_API库一起使用.

我查看了这些文章

https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/prediction/serviceAccount.php
https://code.google.com/p/google-api-php-client/wiki/UsingTheLibrary

https://developers.google.com/storage/docs/authentication#service_accounts
https://developers.google.com/accounts/docs/OAuth2#scenarios

这是我的PHP代码

<?PHP

require_once 'google-api-PHP-client/src/Google_Client.PHP';

const CLIENT_ID = "";
const SERVICE_ACCOUNT_NAME = "";
const KEY_FILE = "super secret path of course ;)";

$client = new Google_Client();

// Loads the key into PKCS 12 format
$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(new Google_AssertionCredentials(
    SERVICE_ACCOUNT_NAME,
    array('https://www.googleapis.com/auth/prediction'),
    $key
  )
);

$client->setClientId(CLIENT_ID);
$auth = $client->authenticate();
print $auth ? "Returned true" : "Returned false";
print "<br>";
print is_null($client->getAccesstoken()) ? "It's null" : "Works";

?>

这是我的输出

Returned true
It’s null

解决方法:

我终于想出了在使用不同资源的混合之后如何使用PHP API库进行身份验证.

这是我的google PHP api libray的身份验证类

<?PHP
require_once 'google-api-PHP-client/src/Google_Client.PHP';
require_once 'google-api-PHP-client/src/contrib/Google_StorageService.PHP';

class Model_Storage_Auth
{
    const CLIENT_ID = "someuniquenumber.apps.googleusercontent.com";
    const SERVICE_ACCOUNT_NAME = "myserviceaccountname@developer.gserviceaccount.com";
    const KEY_FILE = "/supersecretpath/key.p12";
    const ACCESS_TOKEN = 'access_token';
    const APP_NAME = 'My App Name';

    private $google_client;

    function __construct()
    {
        $this->google_client = new Google_Client();
        $this->google_client->setApplicationName(self::APP_NAME);
    }

    public function getToken()
    {
        if(!is_null($this->google_client->getAccesstoken())){}
        elseif(!is_null(Session::get(self::ACCESS_TOKEN, null)))
        {
            $this->google_client->setAccesstoken(Session::get(self::ACCESS_TOKEN, null));
        }
        else
        {
            $scope = array();
            $scope[] = 'https://www.googleapis.com/auth/devstorage.full_control';
            $key = file_get_contents(self::KEY_FILE);
            $this->google_client->setAssertionCredentials(new Google_AssertionCredentials(
                self::SERVICE_ACCOUNT_NAME,
                $scope,
                $key)
            );
            $this->google_client->setClientId(self::CLIENT_ID);
            Google_Client::$auth->refreshTokenWithAssertion();
            $token = $this->google_client->getAccesstoken();
            Session::set(self::ACCESS_TOKEN, $token);
        }
        return $this->google_client->getAccesstoken();
    }

}

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...