@@ -46,15 +46,15 @@ item "File > Project properties".
4646Alternatively you can call the service's ` getCallbackUrl() ` method to view the
4747exact URL that the service will use when performing the OAuth flow:
4848
49- /**
50- * Logs the callback URL to register.
51- */
52- function logCallbackUrl() {
53- var service = getService();
54- Logger.log(service.getCallbackUrl() );
55- }
56-
57-
49+ ``` js
50+ /**
51+ * Logs the callback URL to register.
52+ */
53+ function logCallbackUrl () {
54+ var service = getService ( );
55+ Logger . log ( service . getCallbackUrl ());
56+ }
57+ ```
5858
5959## Usage
6060
@@ -68,27 +68,26 @@ information is not persisted to any data store, so you'll need to create this
6868object each time you want to use it. The example below shows how to create a
6969service for the Twitter API.
7070
71- function getTwitterService() {
72- // Create a new service with the given name. The name will be used when
73- // persisting the authorized token, so ensure it is unique within the
74- // scope of the property store.
75- return OAuth1.createService('twitter')
76- // Set the endpoint URLs.
77- .setAccessTokenUrl('https://api.twitter.com/oauth/access_token')
78- .setRequestTokenUrl('https://api.twitter.com/oauth/request_token')
79- .setAuthorizationUrl('https://api.twitter.com/oauth/authorize')
80-
81- // Set the consumer key and secret.
82- .setConsumerKey('...')
83- .setConsumerSecret('...')
84-
85- // Set the name of the callback function in the script referenced
86- // above that should be invoked to complete the OAuth flow.
87- .setCallbackFunction('authCallback')
88-
89- // Set the property store where authorized tokens should be persisted.
90- .setPropertyStore(PropertiesService.getUserProperties());
91- }
71+ ``` js
72+ function getTwitterService () {
73+ // Create a new service with the given name. The name will be used when
74+ // persisting the authorized token, so ensure it is unique within the
75+ // scope of the property store.
76+ return OAuth1 .createService (' twitter' )
77+ // Set the endpoint URLs.
78+ .setAccessTokenUrl (' https://api.twitter.com/oauth/access_token' )
79+ .setRequestTokenUrl (' https://api.twitter.com/oauth/request_token' )
80+ .setAuthorizationUrl (' https://api.twitter.com/oauth/authorize' )
81+ // Set the consumer key and secret.
82+ .setConsumerKey (' ...' )
83+ .setConsumerSecret (' ...' )
84+ // Set the name of the callback function in the script referenced
85+ // above that should be invoked to complete the OAuth flow.
86+ .setCallbackFunction (' authCallback' )
87+ // Set the property store where authorized tokens should be persisted.
88+ .setPropertyStore (PropertiesService .getUserProperties ());
89+ }
90+ ```
9291
9392### 2. Create a request token and direct the user to the authorization URL
9493
@@ -97,20 +96,22 @@ you'll need to present the authorization URL as a link for the user to click.
9796The service's ` authorize() ` method generates the request token and returns the
9897authorization URL.
9998
100- function showSidebar() {
101- var twitterService = getTwitterService();
102- if (!twitterService.hasAccess()) {
103- var authorizationUrl = twitterService.authorize();
104- var template = HtmlService.createTemplate(
105- '<a href="<?= authorizationUrl ?>" target="_blank">Authorize</a>. ' +
106- 'Reopen the sidebar when the authorization is complete.');
107- template.authorizationUrl = authorizationUrl;
108- var page = template.evaluate();
109- DocumentApp.getUi().showSidebar(page);
110- } else {
111- ...
112- }
113- }
99+ ``` js
100+ function showSidebar () {
101+ var twitterService = getTwitterService ();
102+ if (! twitterService .hasAccess ()) {
103+ var authorizationUrl = twitterService .authorize ();
104+ var template = HtmlService .createTemplate (
105+ ' <a href="<?= authorizationUrl ?>" target="_blank">Authorize</a>. ' +
106+ ' Reopen the sidebar when the authorization is complete.' );
107+ template .authorizationUrl = authorizationUrl;
108+ var page = template .evaluate ();
109+ DocumentApp .getUi ().showSidebar (page);
110+ } else {
111+ // ...
112+ }
113+ }
114+ ```
114115
115116### 3. Handle the callback
116117
@@ -119,15 +120,17 @@ for your service will be invoked. This callback function should pass its
119120request object to the service's ` handleCallback() ` method, and show a message
120121to the user.
121122
122- function authCallback(request) {
123- var twitterService = getTwitterService();
124- var isAuthorized = twitterService.handleCallback(request);
125- if (isAuthorized) {
126- return HtmlService.createHtmlOutput('Success! You can close this tab.');
127- } else {
128- return HtmlService.createHtmlOutput('Denied. You can close this tab');
129- }
130- }
123+ ``` js
124+ function authCallback (request ) {
125+ var twitterService = getTwitterService ();
126+ var isAuthorized = twitterService .handleCallback (request);
127+ if (isAuthorized) {
128+ return HtmlService .createHtmlOutput (' Success! You can close this tab.' );
129+ } else {
130+ return HtmlService .createHtmlOutput (' Denied. You can close this tab' );
131+ }
132+ }
133+ ```
131134
132135** Note:** In an Apps Script UI it's not possible to automatically close a window
133136or tab, so you'll need to direct the user to close it themselves.
@@ -139,11 +142,13 @@ The service's `fetch()` method accepts the same parameters as the built-in
139142[ ` UrlFetchApp.fetch() ` ] ( https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetch(String,Object) )
140143and automatically signs the requests using the OAuth1 token.
141144
142- function makeRequest() {
143- var twitterService = getTwitterService();
144- var response = twitterService.fetch('https://api.twitter.com/1.1/statuses/user_timeline.json');
145- ...
146- }
145+ ``` js
146+ function makeRequest () {
147+ var twitterService = getTwitterService ();
148+ var response = twitterService .fetch (' https://api.twitter.com/1.1/statuses/user_timeline.json' );
149+ // ...
150+ }
151+ ```
147152
148153## Compatiblity
149154
@@ -160,11 +165,13 @@ If you have an access token set and need to remove it from the property store
160165you can remove it with the ` reset() ` function. Before you can call reset you
161166need to set the property store.
162167
163- function clearService(){
164- OAuth1.createService('twitter')
165- .setPropertyStore(PropertiesService.getUserProperties())
166- .reset();
167- }
168+ ``` js
169+ function clearService (){
170+ OAuth1 .createService (' twitter' )
171+ .setPropertyStore (PropertiesService .getUserProperties ())
172+ .reset ();
173+ }
174+ ```
168175
169176#### Setting the request method and parameter location
170177
0 commit comments