com.google.api.client.auth.oauth2.draft10
Class AccessProtectedResource

java.lang.Object
  extended by com.google.api.client.auth.oauth2.draft10.AccessProtectedResource
All Implemented Interfaces:
HttpExecuteInterceptor, HttpRequestInitializer, HttpUnsuccessfulResponseHandler

Deprecated. (scheduled to be removed in 1.9) Use Credential

@Deprecated
public class AccessProtectedResource
extends Object
implements HttpExecuteInterceptor, HttpRequestInitializer, HttpUnsuccessfulResponseHandler

Thread-safe OAuth 2.0 (draft 10) method for specifying and refreshing the access token parameter as a request parameter as specified in Accessing a Protected Resource.

Sample usage, taking advantage that this class implements HttpRequestInitializer:

  public static HttpRequestFactory createRequestFactoryNoRefresh(HttpTransport transport,
      JsonFactory jsonFactory, AccessTokenResponse accessTokenResponse) {
    return transport.createRequestFactory(new AccessProtectedResource(
        accessTokenResponse.accessToken, Method.AUTHORIZATION_HEADER));
  }

  public static HttpRequestFactory createRequestFactory(HttpTransport transport,
      JsonFactory jsonFactory, AccessTokenResponse accessTokenResponse) {
    return transport.createRequestFactory(new AccessProtectedResource(
        accessTokenResponse.accessToken, Method.AUTHORIZATION_HEADER, transport, jsonFactory,
        "https://server.example.com/authorize", "s6BhdRkqt3", "gX1fBat3bV",
        accessTokenResponse.refreshToken));
  }
 

If you need to persist the access token in a data store, override onAccessToken(String).

If you have a custom request initializer, request execute interceptor, or unsuccessful response handler, take a look at the sample usage for HttpExecuteInterceptor and HttpUnsuccessfulResponseHandler, which are interfaces that this class also implements.

Since:
1.4
Author:
Yaniv Inbar

Nested Class Summary
static class AccessProtectedResource.Method
          Deprecated. Method of accessing protected resources.
 
Constructor Summary
AccessProtectedResource(String accessToken, AccessProtectedResource.Method method)
          Deprecated. Constructor that uses a non-expired access token.
AccessProtectedResource(String accessToken, AccessProtectedResource.Method method, HttpTransport transport, JsonFactory jsonFactory, String authorizationServerUrl, String clientId, String clientSecret, String refreshToken)
          Deprecated. Constructor to use to be able to refresh token when an access token expires.
 
Method Summary
protected  boolean executeAccessTokenRequest(AccessTokenRequest request)
          Deprecated. Executes the given access token request and calls setAccessToken(String) to the access token from the response or null for an error response (whose error message is silently ignored).
protected  boolean executeRefreshToken()
          Deprecated. Request a new access token from the authorization endpoint.
 String getAccessToken()
          Deprecated. Returns the access token or null for none.
 String getAuthorizationServerUrl()
          Deprecated. Returns the encoded authorization server URL or null for none.
 String getClientId()
          Deprecated. Returns the client identifier or null for none.
 String getClientSecret()
          Deprecated. Returns the client secret or null for none.
 JsonFactory getJsonFactory()
          Deprecated. Returns the JSON factory to use for parsing response for refresh token request or null for none.
 AccessProtectedResource.Method getMethod()
          Deprecated. Returns the method of accessing protected resources.
 String getRefreshToken()
          Deprecated. Returns the refresh token associated with the access token to be refreshed or null for none.
 HttpTransport getTransport()
          Deprecated. Return the HTTP transport for executing refresh token request or null for none.
 boolean handleResponse(HttpRequest request, HttpResponse response, boolean retrySupported)
          Deprecated. 
 void initialize(HttpRequest request)
          Deprecated.  
 void intercept(HttpRequest request)
          Deprecated. 
protected  void onAccessToken(String accessToken)
          Deprecated. Notifies of a new access token.
 boolean refreshToken()
          Deprecated. Request a new access token from the authorization endpoint, acquiring a lock on the access token so other threads calling getAccessToken() must wait until the new access token has been retrieved.
 void setAccessToken(String accessToken)
          Deprecated. Sets the access token.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AccessProtectedResource

public AccessProtectedResource(String accessToken,
                               AccessProtectedResource.Method method)
Deprecated. 
Constructor that uses a non-expired access token.

Parameters:
accessToken - access token or null for none (does not call setAccessToken(String))
method - method of accessing protected resources

AccessProtectedResource

public AccessProtectedResource(String accessToken,
                               AccessProtectedResource.Method method,
                               HttpTransport transport,
                               JsonFactory jsonFactory,
                               String authorizationServerUrl,
                               String clientId,
                               String clientSecret,
                               String refreshToken)
Deprecated. 
Constructor to use to be able to refresh token when an access token expires.

Parameters:
accessToken - access token or null for none (does not call setAccessToken(String))
method - method of accessing protected resources
transport - HTTP transport for executing refresh token request
jsonFactory - JSON factory to use for parsing response for refresh token request
authorizationServerUrl - encoded authorization server URL
clientId - client identifier
clientSecret - client secret
refreshToken - refresh token associated with the access token to be refreshed or null for none
Method Detail

getAccessToken

public final String getAccessToken()
Deprecated. 
Returns the access token or null for none.


setAccessToken

public final void setAccessToken(String accessToken)
Deprecated. 
Sets the access token.

Parameters:
accessToken - access token or null for none

getMethod

public final AccessProtectedResource.Method getMethod()
Deprecated. 
Returns the method of accessing protected resources.


getTransport

public HttpTransport getTransport()
Deprecated. 
Return the HTTP transport for executing refresh token request or null for none.


getJsonFactory

public JsonFactory getJsonFactory()
Deprecated. 
Returns the JSON factory to use for parsing response for refresh token request or null for none.


getAuthorizationServerUrl

public String getAuthorizationServerUrl()
Deprecated. 
Returns the encoded authorization server URL or null for none.


getClientId

public String getClientId()
Deprecated. 
Returns the client identifier or null for none.


getClientSecret

public String getClientSecret()
Deprecated. 
Returns the client secret or null for none.


getRefreshToken

public String getRefreshToken()
Deprecated. 
Returns the refresh token associated with the access token to be refreshed or null for none.


refreshToken

public final boolean refreshToken()
                           throws IOException
Deprecated. 
Request a new access token from the authorization endpoint, acquiring a lock on the access token so other threads calling getAccessToken() must wait until the new access token has been retrieved.

Returns:
whether a new access token was retrieved
Throws:
IOException

initialize

public final void initialize(HttpRequest request)
                      throws IOException
Deprecated. 
Specified by:
initialize in interface HttpRequestInitializer
Throws:
IOException

intercept

public void intercept(HttpRequest request)
               throws IOException
Deprecated. 

Default implementation checks if there is an access token and sets the access token parameter using the appropriate method. Subclasses may override.

Specified by:
intercept in interface HttpExecuteInterceptor
Throws:
IOException

handleResponse

public boolean handleResponse(HttpRequest request,
                              HttpResponse response,
                              boolean retrySupported)
Deprecated. 

Default implementation checks for a 401 error code and calls refreshToken(). If executeRefreshToken() throws an I/O exception, this implementation will log the exception and return false. Subclasses may override.

Specified by:
handleResponse in interface HttpUnsuccessfulResponseHandler

executeRefreshToken

protected boolean executeRefreshToken()
                               throws IOException
Deprecated. 
Request a new access token from the authorization endpoint.

If the refresh token grant parameter passed to the constructor was null, default implementation uses just always returns false. Otherwise, it uses AccessTokenRequest.RefreshTokenGrant based on the refresh token and then passes it to executeAccessTokenRequest(AccessTokenRequest) to execute.

Subclasses may override. Implementations can assume proper thread synchronization is already taken care of inside refreshToken(), where this is called from.

Returns:
whether a new access token was successfully retrieved
Throws:
IOException - I/O exception

executeAccessTokenRequest

protected final boolean executeAccessTokenRequest(AccessTokenRequest request)
                                           throws IOException
Deprecated. 
Executes the given access token request and calls setAccessToken(String) to the access token from the response or null for an error response (whose error message is silently ignored).

Parameters:
request - access token request
Returns:
whether a new access token was successfully retrieved
Throws:
IOException - any I/O problem except HttpResponseException which is silently handled
Since:
1.5

onAccessToken

protected void onAccessToken(String accessToken)
Deprecated. 
Notifies of a new access token.

Default implementation does nothing, but subclasses may override in order to provide functionality like persisting the access token in a data store. Implementations can assume proper thread synchronization is already taken care of inside setAccessToken(String), where this is called from.

Parameters:
accessToken - access token or null for none


Copyright © 2011-2012 Google. All Rights Reserved.