# Class AuthorizationCodeFlow (1.39.0)

<button class="button button-with-icon cloud-select-dropdown__button " aria-haspopup="true">Version latest</button>

- [1.39.0 (latest)](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.AuthorizationCodeFlow)
- [1.38.2](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/1.38.2/com.google.api.client.auth.oauth2.AuthorizationCodeFlow)
- [1.37.0](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/1.37.0/com.google.api.client.auth.oauth2.AuthorizationCodeFlow)
- [1.36.0](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/1.36.0/com.google.api.client.auth.oauth2.AuthorizationCodeFlow)
- [1.34.1](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/1.34.1/com.google.api.client.auth.oauth2.AuthorizationCodeFlow)
- [1.33.3](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/1.33.3/com.google.api.client.auth.oauth2.AuthorizationCodeFlow)
- [1.32.1](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/1.32.1/com.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)](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.AuthorizationCodeFlow#com_google_api_client_auth_oauth2_AuthorizationCodeFlow_loadCredential_java_lang_String_) based on the known user ID to check
if the end-user's credentials are already known. If not, call [#newAuthorizationUrl()](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.AuthorizationCodeFlow#com_google_api_client_auth_oauth2_AuthorizationCodeFlow_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)](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.AuthorizationCodeFlow#com_google_api_client_auth_oauth2_AuthorizationCodeFlow_newTokenRequest_java_lang_String_). Finally, use [#createAndStoreCredential(TokenResponse, String)](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.AuthorizationCodeFlow#com_google_api_client_auth_oauth2_AuthorizationCodeFlow_createAndStoreCredential_com_google_api_client_auth_oauth2_TokenResponse_java_lang_String_) to store and obtain a credential for accessing
protected resources.

## Inheritance

[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \> AuthorizationCodeFlow

## Inherited Members

[Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) [Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) [Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) [Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) [Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) [Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) [Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) [Object.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) [Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) [Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) [Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)

## Constructors

### AuthorizationCodeFlow(AuthorizationCodeFlow.Builder builder)

    protected AuthorizationCodeFlow(AuthorizationCodeFlow.Builder builder)

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `builder` | `https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.AuthorizationCodeFlow.Builder` authorization code flow builder |

### AuthorizationCodeFlow(Credential.AccessMethod method, HttpTransport transport, JsonFactory jsonFactory, GenericUrl tokenServerUrl, HttpExecuteInterceptor clientAuthentication, String clientId, String authorizationServerEncodedUrl)

    public AuthorizationCodeFlow(Credential.AccessMethod method, HttpTransport transport, JsonFactory jsonFactory, GenericUrl tokenServerUrl, HttpExecuteInterceptor clientAuthentication, String clientId, String authorizationServerEncodedUrl)

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `method` | `https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential.AccessMethod` method of presenting the access token to the resource server (for example [BearerToken#authorizationHeaderAccessMethod](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.BearerToken#com_google_api_client_auth_oauth2_BearerToken_authorizationHeaderAccessMethod_)) |
| `transport` | `com.google.api.client.http.HttpTransport` HTTP transport |
| `jsonFactory` | `com.google.api.client.json.JsonFactory` JSON factory |
| `tokenServerUrl` | `com.google.api.client.http.GenericUrl` token server URL |
| `clientAuthentication` | `com.google.api.client.http.HttpExecuteInterceptor` client authentication or `null` for none (see [TokenRequest#setClientAuthentication(HttpExecuteInterceptor)](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.TokenRequest#com_google_api_client_auth_oauth2_TokenRequest_setClientAuthentication_com_google_api_client_http_HttpExecuteInterceptor_)) |
| `clientId` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` client identifier |
| `authorizationServerEncodedUrl` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` authorization server encoded URL |

## Methods

### createAndStoreCredential(TokenResponse response, String userId)

    public Credential createAndStoreCredential(TokenResponse response, String userId)

Creates a new credential for the given user ID based on the given token response and stores it
in the credential store.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `response` | `https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.TokenResponse` token response |
| `userId` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` user ID or `null` if not using a persisted credential store |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential` | newly created credential |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/io/IOException.html` |   |

### getAuthorizationServerEncodedUrl()

    public final String getAuthorizationServerEncodedUrl()

Returns the authorization server encoded URL.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |   |

### getClientAuthentication()

    public final HttpExecuteInterceptor getClientAuthentication()

Returns the client authentication or `null` for none (see [TokenRequest#setClientAuthentication(HttpExecuteInterceptor)](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.TokenRequest#com_google_api_client_auth_oauth2_TokenRequest_setClientAuthentication_com_google_api_client_http_HttpExecuteInterceptor_)).

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `com.google.api.client.http.HttpExecuteInterceptor` |   |

### getClientId()

    public final String getClientId()

Returns the client identifier.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |   |

### getClock()

    public final Clock getClock()

Returns the clock which will be passed along to the Credential.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `com.google.api.client.util.Clock` |   |

### getCredentialDataStore()

    public final DataStore<StoredCredential> getCredentialDataStore()

Beta   

Returns the stored credential data store or `null` for none.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `com.google.api.client.util.store.DataStore<https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.StoredCredential>` |   |

### getCredentialStore() (deprecated)

    public final CredentialStore getCredentialStore()

**Deprecated.** *(to be removed in the future) Use [#getCredentialDataStore()](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.AuthorizationCodeFlow#com_google_api_client_auth_oauth2_AuthorizationCodeFlow_getCredentialDataStore__) instead.*

Beta   

Returns the credential persistence store or `null` for none.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.CredentialStore` |   |

### getJsonFactory()

    public final JsonFactory getJsonFactory()

Returns the JSON factory.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `com.google.api.client.json.JsonFactory` |   |

### getMethod()

    public final Credential.AccessMethod getMethod()

Returns the method of presenting the access token to the resource server (for example [BearerToken#authorizationHeaderAccessMethod](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.BearerToken#com_google_api_client_auth_oauth2_BearerToken_authorizationHeaderAccessMethod_)).

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential.AccessMethod` |   |

### getRefreshListeners()

    public final Collection<CredentialRefreshListener> getRefreshListeners()

Returns the unmodifiable list of listeners for refresh token results.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html<https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.CredentialRefreshListener>` |   |

### getRequestInitializer()

    public final HttpRequestInitializer getRequestInitializer()

Returns the HTTP request initializer or `null` for none.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `com.google.api.client.http.HttpRequestInitializer` |   |

### getScopes()

    public final Collection<String> getScopes()

Returns the a collection of scopes.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html<https://docs.oracle.com/javase/8/docs/api/java/lang/String.html>` |   |

### getScopesAsString()

    public final String getScopesAsString()

Returns the space-separated list of scopes.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |   |

### getTokenServerEncodedUrl()

    public final String getTokenServerEncodedUrl()

Returns the token server encoded URL.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |   |

### getTransport()

    public final HttpTransport getTransport()

Returns the HTTP transport.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `com.google.api.client.http.HttpTransport` |   |

### loadCredential(String userId)

    public Credential loadCredential(String userId)

Loads the credential of the given user ID from the credential store.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `userId` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` user ID or `null` if not using a persisted credential store |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential` | credential found in the credential store of the given user ID or `null` for none found |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/io/IOException.html` |   |

### newAuthorizationUrl()

    public AuthorizationCodeRequestUrl newAuthorizationUrl()

Returns a new instance of an authorization code request URL.

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()](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.AuthorizationCodeFlow#com_google_api_client_auth_oauth2_AuthorizationCodeFlow_getAuthorizationServerEncodedUrl__), [#getClientId()](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.AuthorizationCodeFlow#com_google_api_client_auth_oauth2_AuthorizationCodeFlow_getClientId__), and [#getScopes()](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.AuthorizationCodeFlow#com_google_api_client_auth_oauth2_AuthorizationCodeFlow_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);
     }
     
| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.AuthorizationCodeRequestUrl` |   |

### newTokenRequest(String authorizationCode)

    public AuthorizationCodeTokenRequest newTokenRequest(String authorizationCode)

Returns a new instance of an authorization code token request based on the given authorization
code.

This is used to make a request for an access token using the authorization code. It uses
[#getTransport()](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.AuthorizationCodeFlow#com_google_api_client_auth_oauth2_AuthorizationCodeFlow_getTransport__), [#getJsonFactory()](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.AuthorizationCodeFlow#com_google_api_client_auth_oauth2_AuthorizationCodeFlow_getJsonFactory__), [#getTokenServerEncodedUrl()](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.AuthorizationCodeFlow#com_google_api_client_auth_oauth2_AuthorizationCodeFlow_getTokenServerEncodedUrl__), [#getClientAuthentication()](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.AuthorizationCodeFlow#com_google_api_client_auth_oauth2_AuthorizationCodeFlow_getClientAuthentication__), [#getRequestInitializer()](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.AuthorizationCodeFlow#com_google_api_client_auth_oauth2_AuthorizationCodeFlow_getRequestInitializer__), and [#getScopes()](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.AuthorizationCodeFlow#com_google_api_client_auth_oauth2_AuthorizationCodeFlow_getScopes__).

static TokenResponse requestAccessToken(AuthorizationCodeFlow flow, String code)
throws IOException, TokenResponseException {
return flow.newTokenRequest(code).setRedirectUri("<https://client.example.com/rd").execute(>);
}

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `authorizationCode` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` authorization code. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest` |   |