@@ -273,35 +273,7 @@ package com.adobe.protocols.oauth2
273
273
if (code != null )
274
274
{
275
275
log . debug ("Authorization code: " + code );
276
-
277
- // set up URL request
278
- var urlRequest: URLRequest = new URLRequest (tokenEndpoint);
279
- var urlLoader: URLLoader = new URLLoader ();
280
- urlRequest. method = URLRequestMethod . POST ;
281
-
282
- // define POST parameters
283
- var urlVariables : URLVariables = new URLVariables ();
284
- urlVariables. grant_type = OAuth2Const. GRANT_TYPE_AUTHORIZATION_CODE ;
285
- urlVariables. code = code ;
286
- urlVariables. redirect_uri = authorizationCodeGrant. redirectUri;
287
- urlVariables. client_id = authorizationCodeGrant. clientId ;
288
- urlVariables. client_secret = authorizationCodeGrant. clientSecret;
289
- urlRequest. data = urlVariables;
290
-
291
- // attach event listeners
292
- urlLoader. addEventListener (Event . COMPLETE , onGetAccessTokenResult);
293
- urlLoader. addEventListener (IOErrorEvent . IO_ERROR , onGetAccessTokenError);
294
- urlLoader. addEventListener (SecurityErrorEvent . SECURITY_ERROR , onGetAccessTokenError);
295
-
296
- // make the call
297
- try
298
- {
299
- urlLoader. load (urlRequest);
300
- } // try statement
301
- catch (error : Error )
302
- {
303
- log . error ("Error loading token endpoint \" " + tokenEndpoint + "\" " );
304
- } // catch statement
276
+ getAccessTokenWithAuthCode(code );
305
277
} // if statement
306
278
else
307
279
{
@@ -311,6 +283,38 @@ package com.adobe.protocols.oauth2
311
283
dispatchEvent (getAccessTokenEvent);
312
284
} // else statement
313
285
} // if statement
286
+ } // onLocationChange
287
+
288
+ function getAccessTokenWithAuthCode(code : String ): void
289
+ {
290
+ // set up URL request
291
+ var urlRequest: URLRequest = new URLRequest (tokenEndpoint);
292
+ var urlLoader: URLLoader = new URLLoader ();
293
+ urlRequest. method = URLRequestMethod . POST ;
294
+
295
+ // define POST parameters
296
+ var urlVariables : URLVariables = new URLVariables ();
297
+ urlVariables. grant_type = OAuth2Const. GRANT_TYPE_AUTHORIZATION_CODE ;
298
+ urlVariables. code = code ;
299
+ urlVariables. redirect_uri = authorizationCodeGrant. redirectUri;
300
+ urlVariables. client_id = authorizationCodeGrant. clientId ;
301
+ urlVariables. client_secret = authorizationCodeGrant. clientSecret;
302
+ urlRequest. data = urlVariables;
303
+
304
+ // attach event listeners
305
+ urlLoader. addEventListener (Event . COMPLETE , onGetAccessTokenResult);
306
+ urlLoader. addEventListener (IOErrorEvent . IO_ERROR , onGetAccessTokenError);
307
+ urlLoader. addEventListener (SecurityErrorEvent . SECURITY_ERROR , onGetAccessTokenError);
308
+
309
+ // make the call
310
+ try
311
+ {
312
+ urlLoader. load (urlRequest);
313
+ } // try statement
314
+ catch (error : Error )
315
+ {
316
+ log . error ("Error loading token endpoint \" " + tokenEndpoint + "\" " );
317
+ } // catch statement
314
318
315
319
function onGetAccessTokenResult(event: Event ): void
316
320
{
@@ -347,11 +351,25 @@ package com.adobe.protocols.oauth2
347
351
348
352
dispatchEvent (getAccessTokenEvent);
349
353
} // onGetAccessTokenError
350
- } // onLocationChange
354
+ } // getAccessTokenWithAuthCode
351
355
352
356
function onStageWebViewComplete(event: Event ): void
353
357
{
354
- log . info ("Auth URL loading complete after " + (new Date (). time - startTime ) + "ms" );
358
+ // Note: Special provision made particularly for Google OAuth 2 implementation for installed
359
+ // applications. Particularly, when we see a certain redirect URI, we must look for the authorization
360
+ // code in the page title as opposed to in the URL. See https://developers.google.com/accounts/docs/OAuth2InstalledApp#choosingredirecturi
361
+ // for more information.
362
+ if (authorizationCodeGrant. redirectUri == OAuth2Const. GOOGLE_INSTALLED_APPLICATION_REDIRECT_URI && event. currentTarget . title . indexOf (OAuth2Const. RESPONSE_TYPE_AUTHORIZATION_CODE ) > 0 )
363
+ {
364
+ var codeString: String = event. currentTarget . title . substring (event. currentTarget . title . indexOf (OAuth2Const. RESPONSE_TYPE_AUTHORIZATION_CODE ));
365
+ var code : String = codeString. split ("=" )[ 1 ];
366
+ log . debug ("Authorization code extracted from page title: " + code );
367
+ getAccessTokenWithAuthCode(code );
368
+ }
369
+ else
370
+ {
371
+ log . info ("Auth URL loading complete after " + (new Date (). time - startTime ) + "ms" );
372
+ }
355
373
} // onStageWebViewComplete
356
374
357
375
function onStageWebViewError(errorEvent: ErrorEvent ): void
0 commit comments