{"id":28372994,"url":"https://github.com/googleworkspace/apps-script-oauth1","last_synced_at":"2025-07-08T19:34:32.638Z","repository":{"id":26447494,"uuid":"29898524","full_name":"googleworkspace/apps-script-oauth1","owner":"googleworkspace","description":"An OAuth1 library for Google Apps Script.","archived":false,"fork":false,"pushed_at":"2024-06-11T10:02:40.000Z","size":1451,"stargazers_count":145,"open_issues_count":14,"forks_count":70,"subscribers_count":22,"default_branch":"main","last_synced_at":"2025-05-29T18:57:41.674Z","etag":null,"topics":["apps-script","google-apps-script","gsuite","oauth"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/googleworkspace.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2015-01-27T05:12:46.000Z","updated_at":"2025-05-29T03:58:34.000Z","dependencies_parsed_at":"2024-01-16T09:28:57.503Z","dependency_job_id":"e16967f7-1561-4ff4-9950-baf536f76373","html_url":"https://github.com/googleworkspace/apps-script-oauth1","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/googleworkspace/apps-script-oauth1","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/googleworkspace%2Fapps-script-oauth1","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/googleworkspace%2Fapps-script-oauth1/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/googleworkspace%2Fapps-script-oauth1/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/googleworkspace%2Fapps-script-oauth1/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/googleworkspace","download_url":"https://codeload.github.com/googleworkspace/apps-script-oauth1/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/googleworkspace%2Fapps-script-oauth1/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261874400,"owners_count":23223111,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apps-script","google-apps-script","gsuite","oauth"],"created_at":"2025-05-29T18:38:44.841Z","updated_at":"2025-06-25T12:31:28.358Z","avatar_url":"https://github.com/googleworkspace.png","language":"JavaScript","readme":"# OAuth1 for Apps Script\n\nOAuth1 for Apps Script is a library for Google Apps Script that provides the\nability to create and authorize OAuth1 tokens. This library uses Apps Script's\nnew [StateTokenBuilder](https://developers.google.com/apps-script/reference/script/state-token-builder)\nand `/usercallback` endpoint to handle the redirects.\n\n*Note:* OAuth1 for Google APIs is\n[deprecated](https://developers.google.com/accounts/docs/OAuth) and scheduled\nto be shut down on April 20, 2015. For accessing Google APIs, use the\n[Apps Script OAuth2 library instead](https://github.com/googlesamples/apps-script-oauth2).\n\n## Setup\n\nThis library is already published as an Apps Script, making it easy to include\nin your project. To add it to your script, do the following in the Apps Script\ncode editor:\n\n1. Click on the menu item \"Resources \u003e Libraries...\"\n2. In the \"Find a Library\" text box, enter the script ID\n   `1CXDCY5sqT9ph64fFwSzVtXnbjpSfWdRymafDrtIZ7Z_hwysTY7IIhi7s` and click the\n   \"Select\" button.\n3. Choose a version in the dropdown box (usually best to pick the latest\n   version).\n4. Click the \"Save\" button.\n\nAlternatively, you can copy and paste the files in the [`/dist`](dist) directory\ndirectly into your script project.\n\n\n## Callback URL\n\nBefore you can start authenticating against an OAuth1 provider, you usually need\nto register your application and retrieve the consumer key and secret. Often\nthese registration screens require you to enter a \"Callback URL\", which is the\nURL that users will be redirected to after they've authorized the token. For\nthis library (and the Apps Script functionality in general) the URL will always\nbe in the following format:\n\n    https://script.google.com/macros/d/{SCRIPT ID}/usercallback\n\nWhere `{SCRIPT ID}` is the ID of the script that is using this library. You\ncan find your script's ID in the Apps Script code editor by clicking on the menu\nitem \"File \u003e Project properties\".\n\nAlternatively you can call the service's `getCallbackUrl()` method to view the\nexact URL that the service will use when performing the OAuth flow:\n\n```js\n/**\n * Logs the callback URL to register.\n */\nfunction logCallbackUrl() {\n  var service = getService_();\n  Logger.log(service.getCallbackUrl());\n}\n```\n\n## Usage\n\nUsing the library to generate an OAuth1 token has the following basic steps.\n\n### 1. Create the OAuth1 service\n\nThe Service class contains the configuration information for a given\nOAuth1 provider, including it's endpoints, consumer keys and secrets, etc. This\ninformation is not persisted to any data store, so you'll need to create this\nobject each time you want to use it. The example below shows how to create a\nservice for the Twitter API.\n\nEnsure the method is private (has an underscore at the end of the name) to\nprevent clients from being able to call the method to read your client ID and\nsecret.\n\n```js\nfunction getTwitterService_() {\n  // Create a new service with the given name. The name will be used when\n  // persisting the authorized token, so ensure it is unique within the\n  // scope of the property store.\n  return OAuth1.createService('twitter')\n      // Set the endpoint URLs.\n      .setAccessTokenUrl('https://api.twitter.com/oauth/access_token')\n      .setRequestTokenUrl('https://api.twitter.com/oauth/request_token')\n      .setAuthorizationUrl('https://api.twitter.com/oauth/authorize')\n      // Set the consumer key and secret.\n      .setConsumerKey('...')\n      .setConsumerSecret('...')\n      // Set the name of the callback function in the script referenced\n      // above that should be invoked to complete the OAuth flow.\n      .setCallbackFunction('authCallback')\n      // Set the property store where authorized tokens should be persisted.\n      .setPropertyStore(PropertiesService.getUserProperties());\n}\n```\n\n### 2. Create a request token and direct the user to the authorization URL\n\nApps Script UI's are not allowed to redirect the user's window to a new URL, so\nyou'll need to present the authorization URL as a link for the user to click.\nThe service's `authorize()` method generates the request token and returns the\nauthorization URL.\n\n```js\nfunction showSidebar() {\n  var twitterService = getTwitterService_();\n  if (!twitterService.hasAccess()) {\n    var authorizationUrl = twitterService.authorize();\n    var template = HtmlService.createTemplate(\n        '\u003ca href=\"\u003c?= authorizationUrl ?\u003e\" target=\"_blank\"\u003eAuthorize\u003c/a\u003e. ' +\n        'Reopen the sidebar when the authorization is complete.');\n    template.authorizationUrl = authorizationUrl;\n    var page = template.evaluate();\n    DocumentApp.getUi().showSidebar(page);\n  } else {\n    // ...\n  }\n}\n```\n\n### 3. Handle the callback\n\nWhen the user completes the OAuth1 flow, the callback function you specified\nfor your service will be invoked. This callback function should pass its\nrequest object to the service's `handleCallback()` method, and show a message\nto the user.\n\n```js\nfunction authCallback(request) {\n  var twitterService = getTwitterService_();\n  var isAuthorized = twitterService.handleCallback(request);\n  if (isAuthorized) {\n    return HtmlService.createHtmlOutput('Success! You can close this tab.');\n  } else {\n    return HtmlService.createHtmlOutput('Denied. You can close this tab');\n  }\n}\n```\n\n**Note:** In an Apps Script UI it's not possible to automatically close a window\nor tab, so you'll need to direct the user to close it themselves.\n\n### 4. Make authorized requests\n\nNow that the service is authorized you can use it to make reqests to the API.\nThe service's `fetch()` method accepts the same parameters as the built-in\n[`UrlFetchApp.fetch()`](https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetch(String,Object))\nand automatically signs the requests using the OAuth1 token.\n\n```js\nfunction makeRequest() {\n  var twitterService = getTwitterService_();\n  var response = twitterService.fetch('https://api.twitter.com/1.1/statuses/user_timeline.json');\n  // ...\n}\n```\n\n## Compatiblity\n\nThis library was designed to work with any OAuth1 provider, but because of small\ndifferences in how they implement the standard it may be that some APIs\naren't compatible. If you find an API that it does't work with, open an issue or\nfix the problem yourself and make a pull request against the source code.\n\n### 3-legged OAuth\n\nThis library was primarily designed to support the\n[3-legged OAuth flow](http://oauthbible.com/#oauth-10a-three-legged), where\nthe end-user visits a web page to grant authorization to your application. The\n\"Usage\" section above describes how to configure the library for this flow.\n\n### 2-legged OAuth\n\nThis library does not currently support the\n[2-legged OAuth flow](http://oauthbible.com/#oauth-10a-two-legged), where\ntokens are generated but the user is not prompted to authorize access.\n\nBe aware that many OAuth providers incorrectly use the term \"2-legged\" when\ndescribing their OAuth flow, when in reality they are using the 1-legged flow,\nwhich this library does support.\n\n### 1-legged OAuth\n\nThis library supports the\n[1-legged OAuth flow](http://oauthbible.com/#oauth-10a-one-legged), where the\nconsumer key and secret are simply used to sign requests to the API endpoints,\nwithout the creation or exchanging of tokens. To use this flow, setup the\nservice with a consumer key and secret (and optionally a token and token secret)\nand use it to call the API endpoint. See the\n[Semantics3 sample](samples/Semantics3.gs) and [Yelp sample](samples/Yelp.gs)\nfor some example usage.\n\n## Other features\n\n#### Resetting the access token\n\nIf you have an access token set and need to remove it from the property store\nyou can remove it with the `reset()` function. Before you can call reset you\nneed to set the property store.\n\n```js\nfunction clearService(){\n  OAuth1.createService('twitter')\n      .setPropertyStore(PropertiesService.getUserProperties())\n      .reset();\n}\n```\n\n#### Setting the request method and parameter location\n\nOAuth1 providers may require that you use a particular HTTP method or parameter\nlocation when performing the OAuth1 flow. You can use the methods `setMethod()`\nand `setParamLocation()` to controls these settings.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoogleworkspace%2Fapps-script-oauth1","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgoogleworkspace%2Fapps-script-oauth1","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoogleworkspace%2Fapps-script-oauth1/lists"}