0

好的,我正在尝试使用 Google Drive SDK 设置我的应用程序。我将其用作服务帐户,因此设置略有不同。这就是我做事的方式:

包括所需文件:

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/auth/Google_AssertionCredentials.php';
require_once "google-api-php-client/src/contrib/Google_Oauth2Service.php";

定义常量:

DEFINE("TRUE_PATH",$_SERVER["DOCUMENT_ROOT"].'path/to/file');
DEFINE("SERVICE_ACCOUNT_EMAIL","somecode@developer.gserviceaccount.com");
DEFINE("DRIVE_SCOPE","https://www.googleapis.com/auth/drive");

功能:

function generateAssertion() {

    $assertionCredentials = new Google_AssertionCredentials();

    $assertionCredentials->serviceAccountName='somecode@developer.gserviceaccount.com';

    $assertionCredentials->privateKey=
    TRUE_PATH.'9e01fd1414aa082fadeec316161eb7028558fbde-privatekey.p12';

    $assertionCredentials->privateKeyPassword = 'notasecret';
    $p12cert = array();
    $file = TRUE_PATH.'9e01fd1414aa082fadeec316161eb7028558fbde-privatekey.p12';
    if (file_exists($file)) {
        try {
            $assertion = $assertionCredentials->generateAssertion();
            return $assertion;
        } catch (Exception $e) {
            d($assertionCredentials);
            return 'Caught exception: ' . $e->getMessage() . "\n";
        }
    } else {
        return "no p12 privatekey file";
    }
}

行 $assertion = $assertionCredentials->generateAssertion(); 给了我一个看起来像这样的代码:

eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiJodHRwczpcL1wvYWNjb3VudHMuZ29vZ2xlLmNvbVwvb1wvb2F1dGgyXC90b2tlbiIsInNjb3BlIjpudWxsLCJpYXQiOjEzNTMzNjk3NzUsImV4cCI6MTM1MzM3MzM3NSwiaXNzIjoiMjY5Mjg1NDU2NjY0QGRldmVsb3Blci5nc2VydmljZWFjY291bnQuY29tIn0.KMBTsGZkhLhddBFtc0PB1qZgtdQYyirhWtYixCYx1zo5Otv9pCUBfvrCXOg3JzR-mzE6zHuCZdmGOD7w_LRwYHJpoo0rdpxDXLjtNCgFZdu1d21cVAnqTkIhMGvdS_K7JP_KioXyi-nBdvZt9IXKaApIdDKhRp-T5HByIeO2ijU

我的问题是,我该如何处理从 generateAssertion() 返回的代码/令牌,以便我可以访问我的驱动器帐户?我尝试将其用作访问令牌和刷新令牌,但没有成功。

如果这是一个愚蠢的问题,我深表歉意,但我是 sdk 的新手,任何帮助将不胜感激。

谢谢!!!

4

1 回答 1

0

您不需要调用 generateAssertion()。相反,只需将凭据提供给客户端实例并让它完成生成断言的工作

$client = new Google_Client();
$client->setAssertionCredentials($assertionCredentials);

有关其他详细信息,请参阅http://code.google.com/p/google-api-php-client/wiki/OAuth2

于 2012-11-20T20:12:34.347 回答