Skip to content

Commit 39776ad

Browse files
author
Yaniv Inbar
committed
Change OAuth 2.0 flow to use access_type=offline&approval_prompt=force
http://codereview.appspot.com/5318076/
1 parent a69c57b commit 39776ad

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

google-api-client-extensions/.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
33
<classpathentry kind="src" path="src/main/java" />
4+
<classpathentry kind="src" path="src/test/java" />
45
<classpathentry kind="con"
56
path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5" />
67
<classpathentry kind="output" path="target/classes" />

google-api-client-extensions/src/main/java/com/google/api/client/googleapis/extensions/auth/helpers/oauth2/draft10/GoogleOAuth2ThreeLeggedFlow.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@
3434
@Inheritance(customStrategy = "complete-table")
3535
public class GoogleOAuth2ThreeLeggedFlow extends OAuth2ThreeLeggedFlow {
3636

37+
/**
38+
* Authorization endpoint to use.
39+
*
40+
* <p>
41+
* This forces the approval prompt to show, and requests offline access in order to guarantee a
42+
* refresh token to be returned.
43+
* </p>
44+
*/
45+
private static final String AUTHORIZATION_ENDPOINT =
46+
new GoogleAuthorizationRequestUrl()
47+
.setAccessType("offline").setApprovalPrompt("force").build();
48+
3749
/**
3850
* Create the flow object with the information provided and generate the authorization url.
3951
*
@@ -50,7 +62,7 @@ public GoogleOAuth2ThreeLeggedFlow(
5062
clientSecret,
5163
scope,
5264
callbackUrl,
53-
GoogleAuthorizationRequestUrl.AUTHORIZATION_SERVER_URL,
65+
AUTHORIZATION_ENDPOINT,
5466
GoogleAccessTokenRequest.AUTHORIZATION_SERVER_URL);
5567
}
5668

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2011 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.client.googleapis.extensions.auth.helpers.oauth2.draft10;
16+
17+
import junit.framework.TestCase;
18+
19+
/**
20+
* Tests {@link GoogleOAuth2ThreeLeggedFlow}.
21+
*
22+
* @author Yaniv Inbar
23+
*/
24+
public class GoogleOAuth2ThreeLeggedFlowTest extends TestCase {
25+
26+
public void testConstructor() {
27+
GoogleOAuth2ThreeLeggedFlow flow = new GoogleOAuth2ThreeLeggedFlow(
28+
"userIdValue", "clientIdValue", "clientSecretValue", "scopeValue", "callbackUrlValue");
29+
assertEquals("https://accounts.google.com/o/oauth2/auth?client_id=clientIdValue&"
30+
+ "redirect_uri=callbackUrlValue&response_type=code&scope=scopeValue&"
31+
+ "access_type=offline&approval_prompt=force", flow.getAuthorizationUrl());
32+
}
33+
}

0 commit comments

Comments
 (0)