public class AuthorizationCodeTokenRequest extends TokenRequest
Use Credential to access protected resources from the resource server using the
TokenResponse returned by TokenRequest.execute(). On error, it will instead throw
TokenResponseException.
Sample usage:
static void requestAccessToken() throws IOException {
try {
TokenResponse response =
new AuthorizationCodeTokenRequest(new NetHttpTransport(), new JacksonFactory(),
new GenericUrl("https://server.example.com/token"), "SplxlOBeZQQYbYS6WxSbIA")
.setRedirectUri("https://client.example.com/rd")
.setClientAuthentication(
new BasicAuthentication("s6BhdRkqt3", "7Fjfp0ZBr1KtDRbnfVdmIw")).execute();
System.out.println("Access token: " + response.getAccessToken());
} catch (TokenResponseException e) {
if (e.getDetails() != null) {
System.err.println("Error: " + e.getDetails().getError());
if (e.getDetails().getErrorDescription() != null) {
System.err.println(e.getDetails().getErrorDescription());
}
if (e.getDetails().getErrorUri() != null) {
System.err.println(e.getDetails().getErrorUri());
}
} else {
System.err.println(e.getMessage());
}
}
}
Some OAuth 2.0 providers don't support BasicAuthentication but instead support
ClientParametersAuthentication. In the above sample code, simply replace the class name
and it will work the same way.
Implementation is not thread-safe.
com.google.api.client.util.GenericData.Flags| Constructor and Description |
|---|
AuthorizationCodeTokenRequest(com.google.api.client.http.HttpTransport transport,
com.google.api.client.json.JsonFactory jsonFactory,
com.google.api.client.http.GenericUrl tokenServerUrl,
java.lang.String code) |
| Modifier and Type | Method and Description |
|---|---|
java.lang.String |
getCode()
Returns the authorization code generated by the authorization server.
|
java.lang.String |
getRedirectUri()
Returns the redirect URI parameter matching the redirect URI parameter in the authorization
request or
null for none. |
AuthorizationCodeTokenRequest |
set(java.lang.String fieldName,
java.lang.Object value) |
AuthorizationCodeTokenRequest |
setClientAuthentication(com.google.api.client.http.HttpExecuteInterceptor clientAuthentication)
Sets the client authentication or
null for none. |
AuthorizationCodeTokenRequest |
setCode(java.lang.String code)
Sets the authorization code generated by the authorization server.
|
AuthorizationCodeTokenRequest |
setGrantType(java.lang.String grantType)
Sets the grant type (
"authorization_code", "password",
"client_credentials", "refresh_token" or absolute URI of the extension grant
type). |
AuthorizationCodeTokenRequest |
setRedirectUri(java.lang.String redirectUri)
Sets the redirect URI parameter matching the redirect URI parameter in the authorization
request or
null for none. |
AuthorizationCodeTokenRequest |
setRequestInitializer(com.google.api.client.http.HttpRequestInitializer requestInitializer)
Sets the HTTP request initializer or
null for none. |
AuthorizationCodeTokenRequest |
setScopes(java.util.Collection<java.lang.String> scopes)
Sets the list of scopes (as specified in Access Token Scope) or
null
for none. |
AuthorizationCodeTokenRequest |
setTokenServerUrl(com.google.api.client.http.GenericUrl tokenServerUrl)
Sets the token server URL.
|
execute, executeUnparsed, getClientAuthentication, getGrantType, getJsonFactory, getRequestInitializer, getScopes, getTokenServerUrl, getTransportclone, entrySet, get, getClassInfo, getUnknownKeys, put, putAll, remove, setUnknownKeysclear, containsKey, containsValue, equals, hashCode, isEmpty, keySet, size, toString, valuespublic AuthorizationCodeTokenRequest(com.google.api.client.http.HttpTransport transport,
com.google.api.client.json.JsonFactory jsonFactory,
com.google.api.client.http.GenericUrl tokenServerUrl,
java.lang.String code)
transport - HTTP transportjsonFactory - JSON factorytokenServerUrl - token server URLcode - authorization code generated by the authorization serverpublic AuthorizationCodeTokenRequest setRequestInitializer(com.google.api.client.http.HttpRequestInitializer requestInitializer)
TokenRequestnull for none.
Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.
setRequestInitializer in class TokenRequestpublic AuthorizationCodeTokenRequest setTokenServerUrl(com.google.api.client.http.GenericUrl tokenServerUrl)
TokenRequestOverriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.
setTokenServerUrl in class TokenRequestpublic AuthorizationCodeTokenRequest setScopes(java.util.Collection<java.lang.String> scopes)
TokenRequestnull
for none.
Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.
setScopes in class TokenRequestscopes - collection of scopes to be joined by a space separator (or a single value
containing multiple space-separated scopes)public AuthorizationCodeTokenRequest setGrantType(java.lang.String grantType)
TokenRequest"authorization_code", "password",
"client_credentials", "refresh_token" or absolute URI of the extension grant
type).
Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.
setGrantType in class TokenRequestpublic AuthorizationCodeTokenRequest setClientAuthentication(com.google.api.client.http.HttpExecuteInterceptor clientAuthentication)
TokenRequestnull for none.
The recommended initializer by the specification is BasicAuthentication. All
authorization servers must support that. A common alternative is
ClientParametersAuthentication. An alternative client authentication method may be
provided that implements HttpRequestInitializer.
This HTTP request execute interceptor is guaranteed to be the last execute interceptor before
the request is executed, and after any execute interceptor set by the
TokenRequest.getRequestInitializer().
Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.
setClientAuthentication in class TokenRequestpublic final java.lang.String getCode()
public AuthorizationCodeTokenRequest setCode(java.lang.String code)
Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.
public final java.lang.String getRedirectUri()
null for none.public AuthorizationCodeTokenRequest setRedirectUri(java.lang.String redirectUri)
null for none.
Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.
public AuthorizationCodeTokenRequest set(java.lang.String fieldName, java.lang.Object value)
set in class TokenRequestCopyright © 2011-2018 Google. All Rights Reserved.