相关疑难解决方法(0)

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 -客户端/维基/使用图书馆

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";

?>
Run Code Online (Sandbox Code Playgroud)

这是我的输出:

返回 true 为 …

php google-authentication google-cloud-storage google-oauth

6
推荐指数
1
解决办法
5817
查看次数

如何使用Google API PHP客户端获取OAuth2访问令牌?

我正在尝试获取OAuth访问令牌以将一些数据导入到融合表中.我正在尝试使用Google API PHP客户端.我已经为此创建了一个服务帐户,并且正在使用代码,主要来自serviceAccount示例:

function access_token()
{
    $client = new Google_Client();
    $client->setAuthClass ('Google_OAuth2');
    // ^ Don't know if this line is required,
    // ^ but it fails just as well without it.
    $client->setApplicationName ('Mysite.dom.ain');
    $client->setAssertionCredentials (new Google_AssertionCredentials
        (   'MANY-NUMBERS-LETTERS-DASHES@developer.gserviceaccount.com',
            array ('https://www.googleapis.com/auth/fusiontables'),
            file_get_contents ('path/to/my/privatekey.p12') ));
    $client->setClientId ('NUMBERS-LETTERS-DASHES.apps.googleusercontent.com');
    $client->authenticate();
    // ^ Also fails equally good with and without this line.
    return $client->getAccessToken();
}
Run Code Online (Sandbox Code Playgroud)

一点调试输出显示$client->authenticate()返回true,但$client->getAcessToken()返回null.没有异常被抛出.我觉得我做的事情从根本上是错误的.如果是这样,请原谅我的愚蠢并指出我正确的方向.

php google-api google-fusion-tables google-oauth

3
推荐指数
1
解决办法
1万
查看次数