|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.google.api.client.auth.oauth2.AuthorizationCodeFlow
public class AuthorizationCodeFlow
Thread-safe OAuth 2.0 authorization code flow that manages and persists end-user credentials.
This is designed to simplify the flow in which an end-user authorizes the application to access their protected data, and then the application has access to their data based on an access token and a refresh token to refresh that access token when it expires.
The first step is to call loadCredential(String) based on the known user ID to check if
the end-user's credentials are already known. If not, call newAuthorizationUrl() and
direct the end-user's browser to an authorization page. The web browser will then redirect to the
redirect URL with a "code" query parameter which can then be used to request an access
token using newTokenRequest(String). Finally, use
createAndStoreCredential(TokenResponse, String) to store and obtain a credential for
accessing protected resources.
| Nested Class Summary | |
|---|---|
static class |
AuthorizationCodeFlow.Builder
Authorization code flow builder. |
| Constructor Summary | |
|---|---|
protected |
AuthorizationCodeFlow(Credential.AccessMethod method,
HttpTransport transport,
JsonFactory jsonFactory,
GenericUrl tokenServerUrl,
HttpExecuteInterceptor clientAuthentication,
String clientId,
String authorizationServerEncodedUrl,
CredentialStore credentialStore,
HttpRequestInitializer requestInitializer,
String scopes)
|
protected |
AuthorizationCodeFlow(Credential.AccessMethod method,
HttpTransport transport,
JsonFactory jsonFactory,
GenericUrl tokenServerUrl,
HttpExecuteInterceptor clientAuthentication,
String clientId,
String authorizationServerEncodedUrl,
CredentialStore credentialStore,
HttpRequestInitializer requestInitializer,
String scopes,
Clock clock)
|
| Method Summary | |
|---|---|
Credential |
createAndStoreCredential(TokenResponse response,
String userId)
Creates a new credential for the given user ID based on the given token response and store in the credential store. |
String |
getAuthorizationServerEncodedUrl()
Returns the authorization server encoded URL. |
HttpExecuteInterceptor |
getClientAuthentication()
Returns the client authentication or null for none (see
TokenRequest.setClientAuthentication(HttpExecuteInterceptor)). |
String |
getClientId()
Returns the client identifier. |
Clock |
getClock()
Returns the clock which will be passed along to the Credential. |
CredentialStore |
getCredentialStore()
Returns the credential persistence store or null for none. |
JsonFactory |
getJsonFactory()
Returns the JSON factory. |
Credential.AccessMethod |
getMethod()
Returns the method of presenting the access token to the resource server (for example BearerToken.authorizationHeaderAccessMethod()). |
HttpRequestInitializer |
getRequestInitializer()
Returns the HTTP request initializer or null for none. |
String |
getScopes()
Returns the space-separated list of scopes or null for none. |
String |
getTokenServerEncodedUrl()
Returns the token server encoded URL. |
HttpTransport |
getTransport()
Returns the HTTP transport. |
Credential |
loadCredential(String userId)
Loads the credential of the given user ID from the credential store. |
AuthorizationCodeRequestUrl |
newAuthorizationUrl()
Returns a new instance of an authorization code request URL. |
AuthorizationCodeTokenRequest |
newTokenRequest(String authorizationCode)
Returns a new instance of an authorization code token request based on the given authorization code. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
protected AuthorizationCodeFlow(Credential.AccessMethod method,
HttpTransport transport,
JsonFactory jsonFactory,
GenericUrl tokenServerUrl,
HttpExecuteInterceptor clientAuthentication,
String clientId,
String authorizationServerEncodedUrl,
CredentialStore credentialStore,
HttpRequestInitializer requestInitializer,
String scopes)
method - method of presenting the access token to the resource server (for example
BearerToken.authorizationHeaderAccessMethod())transport - HTTP transportjsonFactory - JSON factorytokenServerUrl - token server URLclientAuthentication - client authentication or null for none (see
TokenRequest.setClientAuthentication(HttpExecuteInterceptor))clientId - client identifierauthorizationServerEncodedUrl - authorization server encoded URLcredentialStore - credential persistence store or null for nonerequestInitializer - HTTP request initializer or null for nonescopes - space-separated list of scopes or null for none
protected AuthorizationCodeFlow(Credential.AccessMethod method,
HttpTransport transport,
JsonFactory jsonFactory,
GenericUrl tokenServerUrl,
HttpExecuteInterceptor clientAuthentication,
String clientId,
String authorizationServerEncodedUrl,
CredentialStore credentialStore,
HttpRequestInitializer requestInitializer,
String scopes,
Clock clock)
method - method of presenting the access token to the resource server (for example
BearerToken.authorizationHeaderAccessMethod())transport - HTTP transportjsonFactory - JSON factorytokenServerUrl - token server URLclientAuthentication - client authentication or null for none (see
TokenRequest.setClientAuthentication(HttpExecuteInterceptor))clientId - client identifierauthorizationServerEncodedUrl - authorization server encoded URLcredentialStore - credential persistence store or null for nonerequestInitializer - HTTP request initializer or null for nonescopes - space-separated list of scopes or null for noneclock - Clock used for Credential expiration| Method Detail |
|---|
public AuthorizationCodeRequestUrl newAuthorizationUrl()
This is a builder for an authorization web page to allow the end user to authorize the
application to access their protected resources and that returns an authorization code. It uses
the getAuthorizationServerEncodedUrl(), getClientId(), and
getScopes(). Sample usage:
private AuthorizationCodeFlow flow;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
String url = flow.newAuthorizationUrl().setState("xyz")
.setRedirectUri("https://client.example.com/rd").build();
response.sendRedirect(url);
}
public AuthorizationCodeTokenRequest newTokenRequest(String authorizationCode)
This is used to make a request for an access token using the authorization code. It uses
getTransport(), getJsonFactory(), getTokenServerEncodedUrl(),
getClientAuthentication(), getRequestInitializer(), and getScopes().
static TokenResponse requestAccessToken(AuthorizationCodeFlow flow, String code)
throws IOException, TokenResponseException {
return flow.newTokenRequest(code).setRedirectUri("https://client.example.com/rd").execute();
}
authorizationCode - authorization code.
public Credential createAndStoreCredential(TokenResponse response,
String userId)
throws IOException
Upgrade warning: since version 1.10 this method throws an IOException. This was not
done prior to 1.10.
response - token responseuserId - user ID or null if not using a persisted credential store
IOException
public Credential loadCredential(String userId)
throws IOException
Upgrade warning: since version 1.10 this method throws an IOException. This was not
done prior to 1.10.
userId - user ID or null if not using a persisted credential store
null for none
found
IOExceptionpublic final Credential.AccessMethod getMethod()
BearerToken.authorizationHeaderAccessMethod()).
public final HttpTransport getTransport()
public final JsonFactory getJsonFactory()
public final String getTokenServerEncodedUrl()
public final HttpExecuteInterceptor getClientAuthentication()
null for none (see
TokenRequest.setClientAuthentication(HttpExecuteInterceptor)).
public final String getClientId()
public final String getAuthorizationServerEncodedUrl()
public final CredentialStore getCredentialStore()
null for none.
public final HttpRequestInitializer getRequestInitializer()
null for none.
public final String getScopes()
null for none.
public final Clock getClock()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||