如何使用PHP执行简单的Google脚本API [英] How to do simple google script api with php

查看:62
本文介绍了如何使用PHP执行简单的Google脚本API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何通过php的api调用Google脚本.这是我的脚本:

I am trying to figure out how to call a google script via the api from php. Here is my script:

function doGet() {
  return ContentService.createTextOutput("hello world!");
}

我可以通过其URL将其称为网络应用,但我要完成的工作将要求通过API对其进行调用. Google的文档如此混乱,几乎毫无用处.

I can call it as a web app via it's URL but what I am trying to accomplish will require it be called via the API. Google's documentation is so confusing it's almost useless.

这似乎是一个简单的任务,但我很快就没了.有谁知道在哪里可以找到简单的分步教程,或者有人可以详细说明完成此步骤所需的步骤吗?

This seems like it would be such a simple task but I'm getting nowhere fast. Does anyone know where to find a simple step by step tutorial or can someone detail the steps required to accomplish this?

更新:因此,我放弃了Google API来访问脚本.我可以使用所有驱动器api调用,但是似乎无法通过API调用我自己的脚本.这是我的最终目标:

UPDATE: So, I've given up on the google API to access scripts. I can use all of the drive api calls but it seems it's not possible to call my own script via the API. Here is my ultimate goal:

用于压缩文件的Google脚本:

A google script to zip files:

function doGet(e) {
  var fileIds = [];
// hard code a couple of file id's just for simplicity now.
  fileIds.push('0BzoYw0RgVVOvU0RqdTFrcGdSSzA');
  fileIds.push('0BzoYw0RgVVOvNEJNcWpmSkNxNTA');  
  return ContentService.createTextOutput(zipping(fileIds));
}

function zipping(fileIds) {
  var zipfilename = "sample2.zip";
  var blobs = [];
  var mimeInf = [];
  var accesstoken = ScriptApp.getOAuthToken();
  fileIds.forEach(function(e) {
      try {
          var file = DriveApp.getFileById(e);
          var mime = file.getMimeType();
          var name = file.getName();
      } catch (er) {
          return er
      }
      var blob;
          blob = UrlFetchApp.fetch("https://www.googleapis.com/drive/v3/files/" + e + "?alt=media", {
              method: "GET",
              headers: {"Authorization": "Bearer " + accesstoken},
              muteHttpExceptions: true
          }).getBlob().setName(name);
      blobs.push(blob);
  });
  var zip = Utilities.zip(blobs, zipfilename);
  return DriveApp.createFile(zip).getId(); // return the file id of the new zip file.
}

发布->部署为Web应用...

Publish -> Deploy as web app...

选择新建"

选择我"

选择只有我自己"

点击部署"并根据请求授权访问.

Click "Deploy" and authorize the access as requested.

使用curl从命令行调用Web应用进行测试:

Call the web app with curl from command line to test:

curl -L https://script.google.com/macros/u/0/s/### script id ###/exec

获取包含错误的html:抱歉,目前无法打开文件."

Get html containing the error: "Sorry, unable to open the file at this time."

更改权限,以便任何人都可以执行该应用程序.

Change permissions so anyone can execute the app.

相同错误.即使以相同的用户身份在浏览器中输入url也会出现相同的错误.

Same error. Even entering the url in the browser as the same user gives the same error.

在这一点上,我认为我必须承认失败并找到Google以外的解决方案.

At this point I think I have to admit defeat and find a solution other than Google.

------------------更新2 --------------------

------------------ UPDATE 2 --------------------

显然是错误:

很抱歉,目前无法打开文件.请检查地址,然后重试."

"Sorry, unable to open the file at this time. Please check the address and try again."

我认为这是Google的一个奇怪问题.同一脚本在某些帐户中有效,而在其他帐户中则无效.我从网上搜索中看到,其他人随机得到此错误,并且没有确定的解决方案.如果有人知道一个,请告诉我.

I think this is some oddball problem with Google. The same script works in some accounts but not others. I see from searching the web, others randomly get this error and there is no definitive solution. If anyone knows one, please let me know.

-----------------更新3 --------------------

----------------- UPDATE 3 --------------------

此错误是Google的一个奇怪问题.显然,某些帐户中的新脚本将在第二天之前收到此错误.我已经对此进行了多次验证,因此如果您遇到这种情况,请等待一天,然后再次尝试执行.好处是,在Google最终能够打开文件"之后,您可以进行所需的任何更改,包括对该项目的其他脚本文件,并且可以立即进行更新.

This error is an oddball problem with Google. Apparently a new script in some accounts will get this error until the next day. I have verified this several times so if this happens to you, wait a day and try executing it again. The good thing is that after Google finally is able to "open the file" you can make any changes you want, including additional script files to that project, and it updates instantly.

但是,一个新项目将不得不等到第二天,因此请提前创建您认为可能需要的任何内容,并提前一天多花些时间.

However, a new project will have to wait until the next day so pre create any you think you might want and a couple extra a day ahead of time.

推荐答案

该示例脚本如何?部署Web Apps时,请复制并粘贴URL.并将其用于以下脚本的$url.

How about this sample script? When you deploy Web Apps, please copy and paste the URL. And please use it to $url of the following script.

  • 在脚本编辑器上
    • 文件
    • ->管理版本
    • ->保存新版本
    • 发布
    • ->部署为Web应用
    • ->项目版本:"是最新版本或创建为新版本.
    • ->在将应用程序执行为"下,选择您的帐户"
    • ->在有权访问该应用程序的人"中,选择任何人,甚至是匿名的"
    • ->单击部署"
    • ->复制当前Web应用程序URL"
    • ->单击确定"
    • On the Script Editor
      • File
      • -> Manage Versions
      • -> Save New Version
      • Publish
      • -> Deploy as Web App
      • -> "Project version:" is latest one or create as New.
      • -> At "Execute the app as", select "your account"
      • -> At "Who has access to the app", select "Anyone, even anonymous"
      • -> Click "Deploy"
      • -> Copy "Current web app URL"
      • -> Click "OK"
      <?php
      $url = 'https://script.google.com/macros/s/#####/exec';
      $curl = curl_init();
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($curl,CURLOPT_FOLLOWLOCATION, true);
      $response = curl_exec($curl);
      echo $response;
      curl_close($curl);
      ?>
      

      结果:

      hello world!
      

      参考文献:

      • 网络应用
      • cURL函数
      • References :

        • Web Apps
        • cURL Functions
        • 如果我误解了你的问题,对不起.

          If I misunderstand your question, I'm sorry.

          这篇关于如何使用PHP执行简单的Google脚本API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆