⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 googleservice.java

📁 google gdata API 很好用的API
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* Copyright (c) 2006 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.gdata.client;import com.google.gdata.util.common.base.StringUtil;import com.google.gdata.client.http.AuthSubUtil;import com.google.gdata.client.http.GoogleGDataRequest;import com.google.gdata.client.http.GoogleGDataRequest.GoogleCookie;import com.google.gdata.client.http.HttpGDataRequest.AuthToken;import com.google.gdata.data.BaseEntry;import com.google.gdata.data.BaseFeed;import com.google.gdata.data.DateTime;import com.google.gdata.util.AuthenticationException;import com.google.gdata.util.ContentType;import com.google.gdata.util.RedirectRequiredException;import com.google.gdata.util.ServiceException;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLEncoder;import java.security.GeneralSecurityException;import java.security.PrivateKey;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.Map;import java.util.Set;/** * The GoogleService class extends the basic GData {@link Service} * abstraction to add support for authentication. * *  */public class GoogleService extends Service {  // Name of client application accessing Google service  private String applicationName;  // Name of Google service being accessed  private String serviceName;  // Login name of the user  private String username;  // Password of the user  private String password;  // The Google domain name used for authentication  private String domainName;  // The protocol used for authentication  private String loginProtocol;  /**   * The path name of the Google accounts management handler.   */  public static final String GOOGLE_ACCOUNTS_PATH = "/accounts";  /**   * The path name of the Google login handler.   */  public static final String GOOGLE_LOGIN_PATH = "/accounts/ClientLogin";  /**   * Authentication failed, invalid credentials presented to server.   */  public static class InvalidCredentialsException extends AuthenticationException {    public InvalidCredentialsException(String message) {      super(message);    }  }  /**   * Authentication failed, account has been deleted.   */  public static class AccountDeletedException extends AuthenticationException {    public AccountDeletedException(String message) {      super(message);    }  }  /**   * Authentication failed, account has been disabled.   */  public static class AccountDisabledException extends AuthenticationException {    public AccountDisabledException(String message) {      super(message);    }  }  /**   * Authentication failed, account has not been verified.   */  public static class NotVerifiedException extends AuthenticationException {    public NotVerifiedException(String message) {      super(message);    }  }  /**   * Authentication failed, user did not agree to the terms of service.   */  public static class TermsNotAgreedException extends AuthenticationException {    public TermsNotAgreedException(String message) {      super(message);    }  }  /**   * Authentication failed, authentication service not available.   */  public static class ServiceUnavailableException extends      AuthenticationException {    public ServiceUnavailableException(String message) {      super(message);    }  }  /**   * Authentication failed, CAPTCHA requires answering.   */  public static class CaptchaRequiredException extends AuthenticationException {    private String captchaUrl;    private String captchaToken;    public CaptchaRequiredException(String message,                                    String captchaUrl,                                    String captchaToken) {      super(message);      this.captchaToken = captchaToken;      this.captchaUrl = captchaUrl;    }    public String getCaptchaToken() { return captchaToken; }    public String getCaptchaUrl() { return captchaUrl; }  }  /**   * Authentication failed, the token's session has expired.   */  public static class SessionExpiredException extends AuthenticationException {    public SessionExpiredException(String message) {      super(message);    }  }  /**   * The UserToken encapsulates the token retrieved as a result of   * authenticating to Google using a user's credentials.   */  public static class UserToken implements AuthToken {    private String token;    public UserToken(String token) {      this.token = token;    }    /**     * Returns an authorization header to be used for a HTTP request     * for the respective authentication token.     *     * @param requestUrl the URL being requested     * @param requestMethod the HTTP method of the request     * @return the "Authorization" header to be used for the request     */    public String getAuthorizationHeader(URL requestUrl,                                         String requestMethod) {      return "GoogleLogin auth=" + token;    }  }  /**   * Encapsulates the token used by web applications to login on behalf of   * a user.   */  public static class AuthSubToken implements AuthToken {    private String token;    private PrivateKey key;    public AuthSubToken(String token, PrivateKey key) {      this.token = token;      this.key = key;    }    /**     * Returns an authorization header to be used for a HTTP request     * for the respective authentication token.     *     * @param requestUrl the URL being requested     * @param requestMethod the HTTP method of the request     * @return the "Authorization" header to be used for the request     */    public String getAuthorizationHeader(URL requestUrl,                                         String requestMethod) {      try {        return AuthSubUtil.formAuthorizationHeader(token, key, requestUrl,                                                   requestMethod);      } catch (GeneralSecurityException e) {        throw new RuntimeException(e.getMessage());      }    }  }  /**   * Constructs a GoogleService instance connecting to the service with name   * {@code serviceName} for an application with the name   * {@code applicationName}. The default domain (www.google.com) will be   * used to authenticate.   *   * @param serviceName     the name of the Google service to which we are   *                        connecting. Sample names of services might include   *                        "cl" (Calendar), "mail" (GMail), or   *                        "blogger" (Blogger)   * @param applicationName the name of the client application accessing the   *                        service.  Application names should preferably have   *                        the format [company-id]-[app-name]-[app-version].   *                        The name will be used by the Google servers to   *                        monitor the source of authentication.   */  public GoogleService(String serviceName,                       String applicationName) {    this(serviceName, applicationName, "https", "www.google.com");  }  /**   * Constructs a GoogleService instance connecting to the service with name   * {@code serviceName} for an application with the name   * {@code applicationName}.  The service will authenticate at the provided   * {@code domainName}.   *   * @param serviceName     the name of the Google service to which we are   *                        connecting. Sample names of services might include   *                        "cl" (Calendar), "mail" (GMail), or   *                        "blogger" (Blogger)   * @param applicationName the name of the client application accessing the   *                        service. Application names should preferably have   *                        the format [company-id]-[app-name]-[app-version].   *                        The name will be used by the Google servers to   *                        monitor the source of authentication.   * @param protocol        name of protocol to use for authentication   *                        ("http"/"https")   * @param domainName      the name of the domain hosting the login handler   */  public GoogleService(String serviceName,                       String applicationName,                       String protocol,                       String domainName) {    this.serviceName = serviceName;    this.applicationName = applicationName;    this.domainName = domainName;    this.loginProtocol = protocol;    requestFactory = new GoogleGDataRequest.Factory();    if (applicationName != null) {      requestFactory.setHeader("User-Agent",          applicationName + " " + getServiceVersion());    } else {      requestFactory.setHeader("User-Agent", getServiceVersion());    }  }  /**   * Sets the credentials of the user to authenticate requests to the server.   *   * @param username the name of the user (an email address)   * @param password the password of the user   * @throws AuthenticationException if authentication failed.   */  public void setUserCredentials(String username, String password)      throws AuthenticationException {    setUserCredentials(username, password, null, null);  }  /**   * Sets the credentials of the user to authenticate requests to the server.   * A CAPTCHA token and a CAPTCHA answer can also be optionally provided   * to authenticate when the authentication server requires that a   * CAPTCHA be answered.   *   * @param username the name of the user (an email id)   * @param password the password of the user   * @param captchaToken the CAPTCHA token issued by the server   * @param captchaAnswer the answer to the respective CAPTCHA token   * @throws AuthenticationException if authentication failed   */  public void setUserCredentials(String username,                                 String password,                                 String captchaToken,                                 String captchaAnswer)      throws AuthenticationException {    this.username = username;    this.password = password;    String token = getAuthToken(username, password, captchaToken, captchaAnswer,                                serviceName, applicationName);    GoogleGDataRequest.Factory factory =      (GoogleGDataRequest.Factory) requestFactory;    factory.setAuthToken(new UserToken(token));    // Flush any cookies that might contain session info for the previous user    cookies.clear();  }  /**   * Sets the AuthSub token to be used to authenticate a user.   *   * @param token the AuthSub token retrieved from Google   */  public void setAuthSubToken(String token) {    setAuthSubToken(token, null);  }  /**   * Sets the AuthSub token to be used to authenticate a user.  The token   * will be used in secure-mode, and the provided private key will be used   * to sign all requests.   *   * @param token the AuthSub token retrieved from Google   * @param key the private key to be used to sign all requests   */  public void setAuthSubToken(String token,                              PrivateKey key) {    GoogleGDataRequest.Factory factory =      (GoogleGDataRequest.Factory) requestFactory;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -