服务应用程序和Google Analytics API V3:服务器到服务器OAuth2身份验证?

时间:2012-03-25 20:00:47

标签: php google-api oauth-2.0 google-analytics-api jwt

我正在尝试制作一个服务器应用程序来定期从我自己的GA帐户中提取Google Analytics数据。请注意,它是访问我自己的数据的个人服务器端应用程序,即没有最终用户访问此应用程序。

因此,我在Google API Console中将我的应用程序注册为服务应用程序,这为我提供了客户端ID 私钥< / strong>即可。据我所知,服务应用程序不使用应用程序密钥重定向URL ,因为此服务器到服务器身份验证流程中没有最终用户。实际上,Google API控制台没有给我任何机密,也没有提示我提供重定向网址。

不幸的是,我无法弄清楚如何在Google's PHP Client API内验证我的服务应用程序。有关于使用最终用户验证Web应用程序的大量文档。

Google的文档提示it is possible to authenticate server-to-server by signing a JWT request with the private key。我只是无法弄清楚如何在PHP客户端API中做(虽然我已经浏览了源代码,并且definitely a script使用私钥签署了请求。)

我在这里遗漏了什么吗?如何使用我的私钥和Google PHP客户端API对服务应用程序执行身份验证?

为清晰起见而编辑

4 个答案:

答案 0 :(得分:107)

2012年7月21日更新

Google Analytics API V3现在支持由.p12签名的JWT请求返回的OAuth2令牌。也就是说,我们现在可以使用带有服务帐户的Analytics API

目前正在拉动4年的日常指标,只是为了它的地狱。

这是一个快速的'n'脏步骤:

  1. 转到Google API Console并创建新应用

  2. 服务标签中,翻转 Google Analytics 开关

  3. API访问标签中,点击创建OAuth2.0客户端ID

    • 输入您的姓名,上传徽标,然后点击下一步

    • 选择服务帐户选项并按创建客户ID

    • 下载您的私钥

  4. 现在您又回到了 API Access 页面。您会看到一个名为服务帐户的部分,其中包含客户端ID 电子邮件地址

    • 复制电子邮件地址(例如 #### @ developer.gserviceaccount.com

    • 访问您的GA Admin以用户身份将此电子邮件添加到您的媒体资源

    • 这是必须的;否则你会得到神秘的错误。

  5. 通过Github获取最新的Google PHP Client API

    git submodule add https://github.com/google/google-api-php-client.git google-api-php-client-read-only
    
  6. Rock'n'roll(感谢所有关于更新班级名称的提示):

    // api dependencies
    require_once(PATH_TO_API . 'Google/Client.php');
    require_once(PATH_TO_API . 'Google/Service/Analytics.php');
    
    // create client object and set app name
    $client = new Google_Client();
    $client->setApplicationName(APP_NAME); // name of your app
    
    // set assertion credentials
    $client->setAssertionCredentials(
      new Google_Auth_AssertionCredentials(
    
        APP_EMAIL, // email you added to GA
    
        array('https://www.googleapis.com/auth/analytics.readonly'),
    
        file_get_contents(PATH_TO_PRIVATE_KEY_FILE)  // keyfile you downloaded
    
    ));
    
    // other settings
    $client->setClientId(CLIENT_ID);           // from API console
    $client->setAccessType('offline_access');  // this may be unnecessary?
    
    // create service and get data
    $service = new Google_Service_Analytics($client);
    $service->data_ga->get($ids, $startDate, $endDate, $metrics, $optParams);
    
  7. 以下原始解决方法


      

    尽管文档含糊不清,但似乎most Google APIs do not support service accounts yet,包括Google Analytics。他们   无法消化.p12签名的JWT请求返回的OAuth2令牌。所以,   截至目前,您无法使用Google Analytics API V3   服务帐户

         

    解决方法:

         
        
    1. Google API console中,创建客户端应用程序。

    2.   
    3. 按照Google PHP Client API示例中的步骤,使用client_auth_urlclient_id生成client_secret,   和redirect_uri

    4.   
    5. 使用cURL登录Google。 (务必使用cookie文件!)

    6.   
    7. 打开cURL中的client_auth_url并填写表单。确保设置curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);和   curl_setopt($ch, CURLOPT_HEADER, 1);authorization_code   将位于回复的Location:标题中。

    8.   
    9. 使用您的client_idclient_secretredirect_uri以及步骤4中的激活码,向Google's OAuth2 Token machine发送请求。确保在帖子字段中加入grant_type = "authorization_code"

    10.   
    11. Hurray,你现在有一个永不过期的refresh_token,还有一个有效的access_token!使用client_idclient_secretredirect_uriGoogle's OAuth2 Token machine发送请求,   当refresh_token到期时你会access_token,并且你会得到一个   新的。

    12.   

答案 1 :(得分:4)

Google API PHP客户端现在支持trunk上的服务帐户。

实现尚未发布,因此您需要checkout最新版本的PHP客户端。

我准备了一个示例应用程序,演示了如何使用服务帐户来访问Google Prediction API。 要查看示例,请查看examples / prediction / serviceAccount.php或访问: http://code.google.com/p/google-api-php-client/source/browse/trunk/examples/prediction/serviceAccount.php

答案 2 :(得分:2)

如果您使用的是Google's PHP client API,请转到Google API Console,然后点击左侧的API Access

然后Create a Client ID。这将为您提供secret,这是您设置redirect URL的地方。它不会为您提供重定向网址 - 这是应用在验证后将用户发回的网址。

您可以查看other authentication methods

答案 3 :(得分:2)

您可以使用非常有用的php库GAPI(Google AnalyticsAPI PHP接口)来访问没有OAuth的Google Analytics。它易于使用。