我需要在没有任何用户交互的情况下从我的php服务调用我的GAS web应用程序:
- 脚本与项目 关联
- 我从开发者控制台(http://note.io/1dq51gI)获得的所有凭据
- 在php中,我获得访问令牌(令牌可以用于另一个google服务)
- 我尝试发送get查询到我的脚本,总是有401响应(http://puu.sh/hX9VJ/fdce874ff5.png)
- 这是部署设置- http://puu.sh/hXa5h/a4c8ad4ff2.png(作为用户访问应用程序,访问任何人) 在php中,我使用google php客户端库
$client = new Google_Client();
$client->setApplicationName('my app');
$client->setClientId('my-id.apps.googleusercontent.com');
$cred = new Google_Auth_AssertionCredentials(
'me@developer.gserviceaccount.com',
['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive.scripts'],
file_get_contents(__DIR__ . '/google-app-key.p12')
);
$client->setAssertionCredentials($cred);
if($client->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$objToken = json_decode($client->getAccessToken());
$accessToken = $objToken->access_token;
$curl = curl_init();
$header = [];
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: Bearer '.$accessToken;
curl_setopt($curl, CURLOPT_HTTPHEADER,$header);
curl_setopt($curl, CURLOPT_POST,true);
curl_setopt($curl, CURLOPT_URL, 'https://script.google.com/macros/s/script-id/exec?query-params');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, 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($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSLVERSION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
$rest = curl_exec($curl);
$error = curl_error($curl);
$info = curl_getinfo($curl);
curl_close($curl);
我总是得到401回复谷歌,这是一个问题。
谢谢!
从服务器端调用的内容服务脚本不能与"用户访问web应用程序"一起工作。这是因为脚本需要来自用户的google cookie SID、HSID、SSID的额外信息来运行脚本。这就是为什么您可以在像hurl这样的语言中测试代码。看起来这是可能的,但是来自cURL的相同调用将失败。
如果你从服务器调用脚本,它必须设置为"我"执行。你在头中传递的Oauth令牌将用于"谁有权访问应用程序"选项。