从PHP调用Google应用程序脚本,无需Google登录

从PHP调用Google应用程序脚本,无需Google登录,php,google-apps-script,Php,Google Apps Script,我想从PHP调用Google应用程序脚本,以便在Google开发者控制台中启用的Google帐户中创建电子表格。该帐户已启用驱动器API 这是我的PHP脚本: require_once '/google_api/autoload.php'; $client_email = 'my-client-email@developer.gserviceaccount.com'; $private_key = file_get_contents('key_file.p12'); $scopes

我想从PHP调用Google应用程序脚本,以便在Google开发者控制台中启用的Google帐户中创建电子表格。该帐户已启用驱动器API

这是我的PHP脚本:

require_once '/google_api/autoload.php';

$client_email = 'my-client-email@developer.gserviceaccount.com';
$private_key  = file_get_contents('key_file.p12');

$scopes       = array('https://www.googleapis.com/auth/bigquery' , 
                      'https://www.googleapis.com/auth/cloud-platform',
                      'https://www.googleapis.com/auth/drive',
    );


$credentials = new Google_Auth_AssertionCredentials($client_email,
                                                    $scopes,
                                                    $private_key);

try
{
  $client = new Google_Client();
  $client->setAssertionCredentials($credentials);

  if ($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion();
  }
  $token = $client->getAccessToken();
}
catch(Exception $e)
{
  $token = false;
  print_r($e);
}


try {
  $token = json_decode($token);
} catch (Exception $ex) {
  die ('No token');
}

$url = "https://script.google.com/a/macros/myscript-url-details/exec";
$ch          = curl_init();
if ($ch)
{

  $header = array();
  $header[] = 'Content-type: application/json';
  $header[] = 'Authorization: Bearer '.$token->access_token; 

  curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  curl_setopt($ch, CURLOPT_POST, 1); 
  curl_setopt($ch, CURLOPT_URL, $url);

  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
  curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9");


  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);


  curl_setopt($ch, CURLOPT_SSLVERSION, 1); 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HEADER, true);

  $curl_output = curl_exec($ch);                                          
  if (curl_errno($ch)) 
  {
    $curl_error = curl_errno($ch) .": ". curl_error($ch);
  }

  curl_close($ch);

}
else
{
  $curl_output = "Error on Curl";
}

print $curl_output;
现在,应用程序脚本如下所示:

function createSheetAndDisplayLink()
{
  var spreadsheet = SpreadsheetApp.create('report_1');
  var sheet       = spreadsheet.getSheets()[0];//insertSheet();
  var data        = [
                      ['Col1', 'Col2', 'Col3'],
                      ['Field1', 'XXXXX' ,3],
                      ['Field 2', 'XXXXX' ,7]
                    ];
  var range = sheet.getRange(1, 1, 3, 3);
  range.setValues(data);

  url = spreadsheet.getUrl();

  return url;

}

function doGet(e)
{
  url = createSheetAndDisplayLink();
  var content = "Please click on follow link <a href='" + url +"'>Sheet</a>";
  var output = ContentService.createTextOutput(content);    
  return output;
}
我从CURL得到一个输出,说请求的文件不存在

如果我将设置更改为将应用部署为:

Execute the app as: Me
Who has access to the app: "Anyone"
我得到的结果是需要授权

因此,似乎没有进行授权

这方面的任何帮助都会大有裨益。 谢谢

Execute the app as: Me
Who has access to the app: "Anyone"