【发布时间】:2015-05-23 03:58:51
【问题描述】:
我需要在没有任何用户交互的情况下从我的 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 客户端库
这是我的 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);
我总是收到 google 的 401 响应,这是个问题。
谢谢!
【问题讨论】:
-
使用
Authorization: OAuth token标题我有相同的结果..
标签: php oauth google-apps-script