Google関連

Google Apps Script Execution APIを試してみた。

「GAS Station #2」に参加してきました。」で書いた通り、先週GAS(Google Apps Script)のハンズオンイベント「GAS Station #2」に参加し、新しいAPI「Apps Script Execution API」を触ってきたので、どんな感じで使うのかを書いていこうと思います。

Execution APIは“自分が書いたGAS関数を外部から呼び出すAPI”で、複数のGoogleサービスを跨ぐ処理を行いたい場合、一つの関数にまとめてしまえば、単一のエンドポイントから処理を実行することができます。
(Office 365のunified APIに似ているところがありますね。)

参考情報

呼び出すスクリプトの準備

  1. シート名を取得するスクリプトを呼び出すため、適当にスプレッドシートを用意します。シートのIDは下図の通り、URLから取得することができます。
  2. GoogleAppsScriptExecutionAPI_01

  3. https://script.google.com/ から「空のプロジェクト」を作成します。
  4. GoogleAppsScriptExecutionAPI_02

  5. 下記コードを貼り付けた後「myFunction」を実行し、動作確認を行います。
  6. function getSheetNames(sheetId) {
      var ss = SpreadsheetApp.openById(sheetId);
      var sheets = ss.getSheets();
      return sheets.map(function(sheet) {
        return sheet.getName();
      });
    }
    
    function myFunction() {
      Logger.log(getSheetNames("シートID"));
    }

    GoogleAppsScriptExecutionAPI_03

    GoogleAppsScriptExecutionAPI_04

    GoogleAppsScriptExecutionAPI_05

  7. シート名が取得できれば動作確認終了です。
  8. GoogleAppsScriptExecutionAPI_06

API IDとスコープの取得

  1. 公開メニューから「実行可能 API として導入」をクリックします。
  2. GoogleAppsScriptExecutionAPI_07

  3. 実行可能 API として導入画面が表示されたらバージョンを入力し、「導入」ボタンをクリックします。
  4. GoogleAppsScriptExecutionAPI_08

  5. 「スクリプトの新しいバージョンで新しいスコープが検出されました」との警告が表示された場合は、「続行」ボタンをクリックします。
  6. GoogleAppsScriptExecutionAPI_09

  7. 現在の API ID:」欄にAPI IDが表示されるので、メモ帳にコピーしておきます。
  8. GoogleAppsScriptExecutionAPI_10

  9. ファイルメニューから「プロジェクトのプロパティ」をクリックします。
  10. GoogleAppsScriptExecutionAPI_11

  11. スコープ」タブに表示される OAuth スコープをメモ帳にコピーしておきます(今回の場合は https://www.googleapis.com/auth/spreadsheets )。
  12. GoogleAppsScriptExecutionAPI_12

クライアント IDとクライアント シークレットの取得

  1. リソースメニューから「Developers Console プロジェクト」をクリックします。
  2. GoogleAppsScriptExecutionAPI_13

  3. Developers Console プロジェクト画面が表示されたら「このスクリプトが現在関連付けられているプロジェクト」の下にあるリンクをクリックします。
  4. GoogleAppsScriptExecutionAPI_14

  5. Developers Consoleが表示されたら「APIと認証」から「API」を開きます。
  6. API ライブラリの検索ボックスに「Execution」と入力し、「Google Apps Script Execution API」を有効にします。
  7. GoogleAppsScriptExecutionAPI_15

    GoogleAppsScriptExecutionAPI_16

  8. 「APIと認証」から「認証情報」を開きます。
  9. 認証情報を追加」から「OAuth 2.0 クライアント ID」をクリックします。
  10. GoogleAppsScriptExecutionAPI_17

  11. クライアント ID の作成画面が表示されたら下記情報を入力し「作成」ボタンをクリックします。
  12. ※ 通常はここでアプリに合わせた設定を行います。

    • アプリケーションの種類:ウェブ アプリケーション
    • 名前:Sample Client
    • 承認済みの JavaScript 生成元:https://developers.google.com
    • 承認済みのリダイレクト URI:https://developers.google.com/oauthplayground

    GoogleAppsScriptExecutionAPI_18

  13. OAuth クライアント画面に表示される「クライアント ID」と「クライアント シークレット」をメモ帳にコピーし、「OK」ボタンをクリックします。
  14. GoogleAppsScriptExecutionAPI_19

OAuth Playgroundによる動作確認

  1. OAuth Playground」を開きます。
  2. OAuth 2.0 Configurationから「Use your own OAuth credentials」にチェックを入れ、「OAuth Client ID:」欄と「OAuth Client secret:」欄に、コピーしておいたクライアント IDとクライアント シークレットを貼り付けます。
  3. GoogleAppsScriptExecutionAPI_20

  4. Step 1 Select & authorize APIsにある「Input your own scopes」欄にコピーしておいたスコープを貼り付け「Authorize APIs」ボタンをクリックします。
  5. GoogleAppsScriptExecutionAPI_21

  6. 許可のリクエスト画面が表示されたら「許可」ボタンをクリックします。
  7. GoogleAppsScriptExecutionAPI_22

  8. Step 2 Exchange authorization code for tokensにAuthorization codeが表示されたら「Exchange authorization code for tokens」ボタンをクリックします。
  9. GoogleAppsScriptExecutionAPI_23

  10. Step 3 Configure request to APIが表示されるので、下記のように項目を設定し、「Send the request」ボタンをクリックします。
    • HTTP Method:POST
    • Request URI:https://script.googleapis.com/v1/scripts/(API ID):run
    • Content-Type:application/json
    • Enter request body:
    • {
          'function': 'getSheetNames',
          'parameters': '(シートID)',
          'devMode': true
      }

    ※ 各パラメーターの説明は「GASのExecution APIを使ってGASを外部からぶっ叩く」をご参照ください。

    GoogleAppsScriptExecutionAPI_24

    GoogleAppsScriptExecutionAPI_25

  11. 無事にスクリプトが呼び出せれば、JSON形式で結果が返ってきます。
  12. GoogleAppsScriptExecutionAPI_26

というわけで、「GASのExecution APIを使ってGASを外部からぶっ叩く」を見ながらAPIを試してみましたが、特に問題もなく動作確認できました。

ただ、下記のような拡張サービスのURL Shortener APIを使ったコードを呼び出そうとしたところ、

function getShortUrl(url){
  return UrlShortener.Url.insert({longUrl:url}).id
}

下記のPERMISSION_DENIEDエラーが返ってきました。

HTTP/1.1 403 Forbidden
Alternate-protocol: 443:quic,p=1
Content-length: 138
X-xss-protection: 1; mode=block
X-content-type-options: nosniff
Transfer-encoding: chunked
Vary: Origin, X-Origin, Referer
Server: ESF
-content-encoding: gzip
Cache-control: private
Date: Fri, 16 Oct 2015 02:20:59 GMT
X-frame-options: SAMEORIGIN
Alt-svc: quic=":443"; p="1"; ma=604800
Content-type: application/json; charset=UTF-8
Www-authenticate: Bearer realm="https://accounts.google.com/", error="insufficient_scope", scope="https://www.googleapis.com/auth/directory.group https://spreadsheets.google.com/feeds/worksheets/ https://sites.google.com/feeds/ https://apps-apis.google.com/a/feeds/ https://mail.google.com/mail/ https://www.google.com/m8/feeds http://mail.google.com/ https://www.googleapis.com/auth/script.storage https://www.googleapis.com/auth/forms https://apps-apis.google.com/a/feeds https://www.googleapis.com/auth/admin.directory.user https://mail.google.com/mail https://www.googleapis.com/auth/drive https://spreadsheets.google.com/feeds https://www.googleapis.com/auth/script.webapp.deploy https://www.googleapis.com/auth/admin.directory.group https://www.googleapis.com/auth/groups http://sites.google.com/feeds/#readonly https://mail.google.com https://www.google.com/calendar/feeds/ http://mail.google.com https://www.google.com/calendar/feeds/default/owncalendars/full https://apps-apis.google.com/a/feeds/alias/ https://www.googleapis.com/auth/forms.currentonly https://www.googleapis.com/auth/directory.user https://www.googleapis.com/auth/userinfo#email https://www.google.com/m8/feeds/contacts/default/full https://www.googleapis.com/auth/gpa https://www.googleapis.com/auth/script.send_mail http://sites.google.com/feeds https://www.google.com/m8/feeds/groups/ https://www.googleapis.com/auth/calendar https://www.google.com/m8/feeds/photos/ tag:google.com,2010:auth/groups2 https://mail.google.com/mail/feed/atom https://www.googleapis.com/auth/sqlservice https://www.googleapis.com/auth/documents https://www.google.com/m8/feeds/contacts https://www.google.com/m8/feeds/groups https://www.google.com/m8/feeds/profiles https://spreadsheets.google.com/feeds/spreadsheets https://sites.google.com/feeds/#readonly https://www.googleapis.com/auth/dynamiccreatives https://apps-apis.google.com/a/feeds/groups/ https://www.googleapis.com/auth/script.cpanel http://sites.google.com/feeds/ https://www.googleapis.com/auth/contacts https://www.googleapis.com/auth/script.external_request https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/spreadsheets https://www.googleapis.com/auth/apps.directory.user http://spreadsheets.google.com/feeds/ https://www-opensocial.googleusercontent.com/api/people/@me/@self https://mail.google.com/ https://www.googleapis.com/auth/apps.directory.group https://sites.google.com/feeds https://www.google.com/m8/feeds/photos https://www.google.com/m8/feeds/profiles/ http://www.google.com/calendar/feeds/default/allcalendars/full https://mail.google.com/a/ https://www.google.com/m8/feeds/ https://mail.google.com/mail/feed/atom/ https://www.googleapis.com/auth/script.scriptapp https://www.googleapis.com/auth/documents.currentonly https://www.google.com/calendar/feeds https://www.google.com/m8/feeds/contacts/ https://spreadsheets.google.com/feeds/ https://www.googleapis.com/auth/spreadsheets.currentonly"
{
  "error": {
    "status": "PERMISSION_DENIED", 
    "message": "Request had insufficient authentication scopes.", 
    "code": 403
  }
}

GoogleAppsScriptExecutionAPI_27

Method scripts.run」にも書いてありますが、現時点ではすべてのスコープに対応しているわけでは無さそうです。
このあたりは今後に期待したいところですね!


2015/11/19 追記:
VBAからの呼び出しについて記事を書きました。

・VBAマクロからGoogle Apps Script Execution APIを呼び出す。
//www.ka-net.org/blog/?p=6415

Microsoft Edgeのショートカットをデスクトップに作成する。前のページ

「できたてしずおか茶」が美味しい。次のページ

関連記事

  1. Excel

    Google TTSで文字列を読み上げるマクロ(言語自動検出対応版)

    2012/2/15 追記:下記マクロをExcel 2007/201…

  2. Google関連

    Google AdSenseで「PC のパフォーマンスが低下しています」を表示しないようにする。

    下記のサイトでも話題になっていますが、Google AdSenseの怪…

  3. Google関連

    以前のGoogle マップを使う。

    「「新しい Google マップ」正式公開、地図大きく、精緻すぎる3D…

  4. Google関連

    [Google Apps Script]サイドバーを表示する

    Google Apps Scriptでサイドバーを表示します。…

  5. Google関連

    [Google Apps Script]スクリプト エディタで使えるショートカットキー一覧

    スクリプト エディタで使用できるショートカットキーをまとめてみました。…

  6. Google関連

    スプレッドシートをWeb APIにできるサービスを使ってみた

    ※ 本記事は@kuwazzyさんの記事に続く「Web API Adve…

コメント

  • コメント (0)

  • トラックバックは利用できません。

  1. この記事へのコメントはありません。

Time limit is exhausted. Please reload CAPTCHA.

※本ページはプロモーションが含まれています。

Translate

最近の記事

アーカイブ

PAGE TOP