All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.api.client.auth.oauth.AbstractOAuthGetToken Maven / Gradle / Ivy

Go to download

Google API Client Library for Java. Supports Java 5 (or higher) desktop (SE) and web (EE), Android, and Google App Engine.

There is a newer version: 1.4.1-beta
Show newest version
/*
 * Copyright (c) 2010 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.api.client.auth.oauth;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpResponseException;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.UrlEncodedParser;

import java.io.IOException;

/**
 * Generic OAuth 1.0a URL to request a temporary or long-lived token from an
 * authorization server.
 * 
 * @since 1.0
 * @author Yaniv Inbar
 */
public abstract class AbstractOAuthGetToken extends GenericUrl {

  /**
   * Required identifier portion of the client credentials (equivalent to a
   * username).
   */
  public String consumerKey;

  /** Required OAuth signature algorithm. */
  public OAuthSigner signer;

  /** {@code true} for POST request or the default {@code false} for GET request. */
  protected boolean usePost;

  /**
   * @param authorizationServerUrl encoded authorization server URL
   */
  protected AbstractOAuthGetToken(String authorizationServerUrl) {
    super(authorizationServerUrl);
  }

  /**
   * Executes the HTTP request for a temporary or long-lived token.
   * 
   * @return OAuth credentials response object
   * @throws HttpResponseException for an HTTP error code
   * @throws IOException I/O exception
   */
  public final OAuthCredentialsResponse execute() throws IOException {
    HttpTransport transport = new HttpTransport();
    createParameters().signRequestsUsingAuthorizationHeader(transport);
    HttpRequest request =
        this.usePost ? transport.buildPostRequest() : transport
            .buildGetRequest();
    request.url = this;
    HttpResponse response = request.execute();
    response.disableContentLogging = true;
    OAuthCredentialsResponse oauthResponse = new OAuthCredentialsResponse();
    UrlEncodedParser.parse(response.parseAsString(), oauthResponse);
    return oauthResponse;
  }

  /**
   * Returns a new instance of the OAuth authentication provider. Subclasses may
   * override by calling this super implementation and then adding OAuth
   * parameters.
   */
  public OAuthParameters createParameters() {
    OAuthParameters result = new OAuthParameters();
    result.consumerKey = this.consumerKey;
    result.signer = this.signer;
    return result;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy