Google API服务器到服务器身份验证返回一个空数组?

时间:2014-12-20 20:03:22

标签: php laravel-4 google-api google-calendar-api google-authentication

我正在尝试使用服务器到服务器身份验证访问电子邮件日历,我设法获得一个令牌,但当我使用$service->calendarList->listCalendarList()->getItems()时,它返回一个空数组,我确信那里有一个日历

注意:我正在使用laravel,我已经在Google Admin(广泛域名)的安全选项中添加了我的api密钥

class Calendar {
private static $clientId;
private static $emailAddress;
private static $certificateFingerprints;
private static $p12;
private static $scopes;
private static $p12Password;
private static $sub;
private static $grantType;
private static $client;

public static function initiate(){
    self::$clientId = $_ENV["google_clientId"];
    self::$emailAddress = $_ENV["google_email"];
    self::$certificateFingerprints = $_ENV["google_certificate_fingerprints"];
    self::$p12 = file_get_contents(storage_path("keys/trotride.p12"));
    self::$scopes = array(
        "https://www.googleapis.com/auth/userinfo.email",
        "https://www.googleapis.com/auth/userinfo.profile",
        "https://www.googleapis.com/auth/calendar",
        "https://www.googleapis.com/auth/calendar.readonly",
        "https://www.google.com/m8/feeds/"
    );
    self::$p12Password = "notasecret";
    self::$sub = "mail@domain.net";
    self::$grantType = "http://oauth.net/grant_type/jwt/1.0/bearer";
}
public static function connect(){
    self::initiate();

    $client = new Google_Client();
    $client->setApplicationName("AppName");
    $client->setClientId(self::$clientId);

    $token = Session::get("token");
    if(isset($token)){
        $client->setAccessToken($token);
    }

    $credentials = new Google_Auth_AssertionCredentials(
        self::$emailAddress,
        self::$scopes,
        self::$p12,
        self::$p12Password,
        self::$grantType,
        self::$sub
    );

    $client->setAssertionCredentials($credentials);

    if ($client->getAuth()->isAccessTokenExpired()) {
        $client->getAuth()->refreshTokenWithAssertion();
    }

    if($client->getAccessToken()){
        Session::set("token",$client->getAccessToken());
        self::$client = $client;

        $service = new Google_Service_Calendar($client);
        echo "<pre>";
        var_dump($service->calendarList->listCalendarList()->getItems());
        echo "</pre>";

    }else{
        echo "failed";
    }

}}

变量$ _ENV [&#34; google_email&#34;]包含*********@developer.gserviceaccount.com电子邮件地址

0 个答案:

没有答案