|
| 1 | +/** |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * @OnlyCurrentDoc Limits the add-on to only accessing the current spreadsheet. |
| 19 | + */ |
| 20 | + |
| 21 | +/* |
| 22 | + * The GitHub OAuth2 client ID and secret. |
| 23 | + */ |
| 24 | +var CLIENT_ID = '...'; |
| 25 | +var CLIENT_SECRET = '...'; |
| 26 | + |
| 27 | +/** |
| 28 | + * Adds a custom menu with items to show the sidebar. |
| 29 | + * @param {Object} e The event parameter for a simple onOpen trigger. |
| 30 | + */ |
| 31 | +function onOpen(e) { |
| 32 | + SpreadsheetApp.getUi() |
| 33 | + .createAddonMenu() |
| 34 | + .addItem('Show Sidebar', 'showSidebar') |
| 35 | + .addToUi(); |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Runs when the add-on is installed; calls onOpen() to ensure menu creation and |
| 40 | + * any other initializion work is done immediately. |
| 41 | + * @param {Object} e The event parameter for a simple onInstall trigger. |
| 42 | + */ |
| 43 | +function onInstall(e) { |
| 44 | + onOpen(e); |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Opens a sidebar. The sidebar structure is described in the Sidebar.html |
| 49 | + * project file. |
| 50 | + */ |
| 51 | +function showSidebar() { |
| 52 | + var service = getGitHubService(); |
| 53 | + var template = HtmlService.createTemplateFromFile('Sidebar'); |
| 54 | + template.email = Session.getEffectiveUser().getEmail(); |
| 55 | + template.isSignedIn = service.hasAccess(); |
| 56 | + var page = template.evaluate() |
| 57 | + .setTitle('Sidebar') |
| 58 | + .setSandboxMode(HtmlService.SandboxMode.IFRAME); |
| 59 | + SpreadsheetApp.getUi().showSidebar(page); |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Builds and returns the authorization URL from the service object. |
| 64 | + * @return {String} The authorization URL. |
| 65 | + */ |
| 66 | +function getAuthorizationUrl() { |
| 67 | + return getGitHubService().getAuthorizationUrl(); |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Resets the API service, forcing re-authorization before |
| 72 | + * additional authorization-required API calls can be made. |
| 73 | + */ |
| 74 | +function signOut() { |
| 75 | + getGitHubService().reset(); |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Gets the user's GitHub profile. |
| 80 | + */ |
| 81 | +function getGitHubProfile() { |
| 82 | + var service = getGitHubService(); |
| 83 | + if (!service.hasAccess()) { |
| 84 | + throw 'Error: Missing GitHub authorization.'; |
| 85 | + } |
| 86 | + var url = 'https://api.github.com/user'; |
| 87 | + var response = UrlFetchApp.fetch(url, { |
| 88 | + headers: { |
| 89 | + Authorization: 'Bearer ' + service.getAccessToken() |
| 90 | + } |
| 91 | + }); |
| 92 | + return JSON.parse(response.getContentText()); |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Gets the user's GitHub repos. |
| 97 | + */ |
| 98 | +function getGitHubRepos() { |
| 99 | + var service = getGitHubService(); |
| 100 | + if (!service.hasAccess()) { |
| 101 | + throw 'Error: Missing GitHub authorization.'; |
| 102 | + } |
| 103 | + var url = 'https://api.github.com/user/repos'; |
| 104 | + var response = UrlFetchApp.fetch(url, { |
| 105 | + headers: { |
| 106 | + Authorization: 'Bearer ' + service.getAccessToken() |
| 107 | + } |
| 108 | + }); |
| 109 | + return JSON.parse(response.getContentText()); |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Gets an OAuth2 service configured for the GitHub API. |
| 114 | + * @return {OAuth2.Service} The OAuth2 service |
| 115 | + */ |
| 116 | +function getGitHubService() { |
| 117 | + return OAuth2.createService('github') |
| 118 | + // Set the endpoint URLs. |
| 119 | + .setAuthorizationBaseUrl('https://github.com/login/oauth/authorize') |
| 120 | + .setTokenUrl('https://github.com/login/oauth/access_token') |
| 121 | + |
| 122 | + // Set the client ID and secret. |
| 123 | + .setClientId(CLIENT_ID) |
| 124 | + .setClientSecret(CLIENT_SECRET) |
| 125 | + |
| 126 | + // Set the name of the callback function that should be invoked to complete |
| 127 | + // the OAuth flow. |
| 128 | + .setCallbackFunction('authCallback') |
| 129 | + |
| 130 | + // Set the property store where authorized tokens should be persisted. |
| 131 | + .setPropertyStore(PropertiesService.getUserProperties()) |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * Callback handler that is executed after an authorization attempt. |
| 136 | + * @param {Object} request The results of API auth request. |
| 137 | + */ |
| 138 | +function authCallback(request) { |
| 139 | + var template = HtmlService.createTemplateFromFile('Callback'); |
| 140 | + template.email = Session.getEffectiveUser().getEmail(); |
| 141 | + template.isSignedIn = false; |
| 142 | + template.error = null; |
| 143 | + var title; |
| 144 | + try { |
| 145 | + var service = getGitHubService(); |
| 146 | + var authorized = service.handleCallback(request); |
| 147 | + template.isSignedIn = authorized; |
| 148 | + title = authorized ? 'Access Granted' : 'Access Denied'; |
| 149 | + } catch (e) { |
| 150 | + template.error = e; |
| 151 | + title = 'Access Error'; |
| 152 | + } |
| 153 | + template.title = title; |
| 154 | + return template.evaluate() |
| 155 | + .setTitle(title) |
| 156 | + .setSandboxMode(HtmlService.SandboxMode.IFRAME); |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * Logs the redict URI to register in the Google Developers Console. |
| 161 | + */ |
| 162 | +function logRedirectUri() { |
| 163 | + var service = getGitHubService(); |
| 164 | + Logger.log(service.getRedirectUri()); |
| 165 | +} |
| 166 | + |
| 167 | +/** |
| 168 | + * Includes the given project HTML file in the current HTML project file. |
| 169 | + * Also used to include JavaScript. |
| 170 | + * @param {String} filename Project file name. |
| 171 | + */ |
| 172 | +function include(filename) { |
| 173 | + return HtmlService.createHtmlOutputFromFile(filename).getContent(); |
| 174 | +} |
0 commit comments