通过PHP授权在没有用户交互的情况下访问BigQuery应用程序

时间:2012-11-28 14:47:08

标签: google-api google-bigquery google-api-php-client

我有一个网站,可以轻松地从云端的Google BigQuery中获取USN。现在,在每次访问时,我需要拥有一个Google帐户以及该特定的电子邮件ID,并且应该在“团队选项卡”中分配“权限”。我基本上希望每个人都可以访问该网站。我正在使用PHP,所以我需要有关这里有用的功能的帮助。

2 个答案:

答案 0 :(得分:0)

构建有权访问BigQuery API的应用程序(而不是需要用户授权步骤)的最佳方法是使用OAuth2服务帐户。实质上,服务帐户授权为“服务器到服务器”交互提供基于证书的授权(例如,您的服务器端PHP脚本与BigQuery API交互)。

有使用Google PHP客户端库和服务帐户授权here的文档。

答案 1 :(得分:0)

您必须创建一个'服务帐户'通过谷歌控制台并下载服务器上的私钥文件(.p12)。并使用以下代码

/**
   * setup service client using key file
   * @param String $keyFileLoc location of .p12 file provided in google console
   * @param String $clientEmail client location provided in google console
   */
   function setupServiceClient($keyFileLoc,$clientEmail){
       $client = new Google_Client();
       $service = new Google_Service_Bigquery($client);
       $client->setApplicationName("My Application Name");
       $key = file_get_contents($keyFileLoc);
       $cred = new Google_Auth_AssertionCredentials(
           $clientEmail,
           array('https://www.googleapis.com/auth/bigquery'),
           $key
       ); 
       $this->client->setAssertionCredentials($cred);
       return $service;
  }
相关问题