1- // Examples from https://github.com/googlesamples /apps-script-oauth2
1+ // Examples from https://github.com/googleworkspace /apps-script-oauth2
22
33/**
44 * Create the OAuth2 service.
@@ -17,6 +17,73 @@ function getDriveService() {
1717 . setParam ( "approval_prompt" , "force" ) ;
1818}
1919
20+ /**
21+ * Create sample service for Twitter.
22+ */
23+ function getTwitterService ( ) {
24+ const userProps = PropertiesService . getUserProperties ( ) ;
25+ const clientId = "sampleClientId" ;
26+ const clientSecret = "sampleClientSecret" ;
27+
28+ return OAuth2 . createService ( "Twitter" )
29+ . setAuthorizationBaseUrl ( "https://twitter.com/i/oauth2/authorize" )
30+ . setTokenUrl ( "https://api.twitter.com/2/oauth2/token" )
31+ . setClientId ( clientId )
32+ . setClientSecret ( clientSecret )
33+ . setCallbackFunction ( "authCallback" )
34+ . setPropertyStore ( userProps )
35+ . setScope ( "users.read tweet.read offline.access" )
36+ . generateCodeVerifier ( )
37+ . setTokenHeaders ( {
38+ "Authorization" : "Basic " + Utilities . base64Encode ( clientId + ":" + clientSecret ) ,
39+ "Content-Type" : "application/x-www-form-urlencoded" ,
40+ } ) ;
41+ }
42+
43+ /**
44+ * Create sample service with custom token method.
45+ */
46+ function getTestClientWithCustomTokenMethod ( ) {
47+ return OAuth2 . createService ( "TestService" )
48+ . setAuthorizationBaseUrl ( "https://example.com/auth" )
49+ . setTokenUrl ( "https://example.com/token" )
50+ . setClientId ( "sampleClientId" )
51+ . setClientSecret ( "sampleClientSecret" )
52+ . setCallbackFunction ( "authCallback" )
53+ . setPropertyStore ( PropertiesService . getUserProperties ( ) )
54+ . setTokenMethod ( "PUT" ) ;
55+ }
56+
57+ /**
58+ * Create sample service with code verifier and S256 code challenge method.
59+ */
60+ function getTestClientWithAutoCodeVerifierAndS256ChallengeMethod ( ) {
61+ return OAuth2 . createService ( "TestService" )
62+ . setAuthorizationBaseUrl ( "https://example.com/auth" )
63+ . setTokenUrl ( "https://example.com/token" )
64+ . setClientId ( "sampleClientId" )
65+ . setClientSecret ( "sampleClientSecret" )
66+ . setCallbackFunction ( "authCallback" )
67+ . setPropertyStore ( PropertiesService . getUserProperties ( ) )
68+ . generateCodeVerifier ( )
69+ . setCodeChallengeMethod ( GoogleAppsScriptOAuth2 . CodeChallengeMethod . S256 ) ;
70+ }
71+
72+ /**
73+ * Create sample service with code verifier and S256 code challenge method.
74+ */
75+ function getTestClientWithManualCodeVerifierAndPlainChallengeMethod ( ) {
76+ return OAuth2 . createService ( "TestService" )
77+ . setAuthorizationBaseUrl ( "https://example.com/auth" )
78+ . setTokenUrl ( "https://example.com/token" )
79+ . setClientId ( "sampleClientId" )
80+ . setClientSecret ( "sampleClientSecret" )
81+ . setCallbackFunction ( "authCallback" )
82+ . setPropertyStore ( PropertiesService . getUserProperties ( ) )
83+ . setCodeVerififer ( "sampleCodeVerifier" )
84+ . setCodeChallengeMethod ( GoogleAppsScriptOAuth2 . CodeChallengeMethod . PLAIN ) ;
85+ }
86+
2087/**
2188 * Handle the callback.
2289 */
0 commit comments