Inital commit

This commit is contained in:
Tilman
2011-10-26 09:44:02 +02:00
commit 5971f4b417
1813 changed files with 737837 additions and 0 deletions
+167
View File
@@ -0,0 +1,167 @@
/* Copyright (c) 2008 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 sample.oauth;
import com.google.gdata.client.GoogleService;
import com.google.gdata.client.authn.oauth.GoogleOAuthHelper;
import com.google.gdata.client.authn.oauth.GoogleOAuthParameters;
import com.google.gdata.client.authn.oauth.OAuthHmacSha1Signer;
import com.google.gdata.client.authn.oauth.OAuthRsaSha1Signer;
import com.google.gdata.client.authn.oauth.OAuthSigner;
import com.google.gdata.data.BaseEntry;
import com.google.gdata.data.BaseFeed;
import com.google.gdata.data.Feed;
import java.net.URL;
/**
* Sample application using OAuth in the Google Data Java Client. See the
* comments below to learn about the details.
*
*
*/
class OAuthExample {
public static void main(String[] args) throws Exception {
////////////////////////////////////////////////////////////////////////////
// STEP 1: Gather the user's information
////////////////////////////////////////////////////////////////////////////
// This step collects information from the user, such as the consumer key
// and which service to query. This is just a general setup routine, and
// the method by which you collect user information may be different in your
// implementation.
UserInputHelper inputController = new OAuthUserInputHelper();
UserInputVariables variables = inputController.getVariables();
////////////////////////////////////////////////////////////////////////////
// STEP 2: Set up the OAuth objects
////////////////////////////////////////////////////////////////////////////
// You first need to initialize a few OAuth-related objects.
// GoogleOAuthParameters holds all the parameters related to OAuth.
// OAuthSigner is responsible for signing the OAuth base string.
GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
// Set your OAuth Consumer Key (which you can register at
// https://www.google.com/accounts/ManageDomains).
oauthParameters.setOAuthConsumerKey(variables.getConsumerKey());
// Initialize the OAuth Signer. If you are using RSA-SHA1, you must provide
// your private key as a Base-64 string conforming to the PKCS #8 standard.
// Visit http://code.google.com/apis/gdata/authsub.html#Registered to learn
// more about creating a key/certificate pair. If you are using HMAC-SHA1,
// you must set your OAuth Consumer Secret, which can be obtained at
// https://www.google.com/accounts/ManageDomains.
OAuthSigner signer;
switch (variables.getSignatureMethod()) {
case RSA:
signer = new OAuthRsaSha1Signer(variables.getSignatureKey());
break;
case HMAC:
oauthParameters.setOAuthConsumerSecret(variables.getSignatureKey());
signer = new OAuthHmacSha1Signer();
break;
default:
throw new IllegalArgumentException("Invalid Signature Method");
}
// Finally create a new GoogleOAuthHelperObject. This is the object you
// will use for all OAuth-related interaction.
GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(signer);
////////////////////////////////////////////////////////////////////////////
// STEP 3: Get the Authorization URL
////////////////////////////////////////////////////////////////////////////
// Set the scope for this particular service.
oauthParameters.setScope(variables.getScope());
// This method also makes a request to get the unauthorized request token,
// and adds it to the oauthParameters object, along with the token secret
// (if it is present).
oauthHelper.getUnauthorizedRequestToken(oauthParameters);
// Get the authorization url. The user of your application must visit
// this url in order to authorize with Google. If you are building a
// browser-based application, you can redirect the user to the authorization
// url.
String requestUrl = oauthHelper.createUserAuthorizationUrl(oauthParameters);
System.out.println(requestUrl);
System.out.println("Please visit the URL above to authorize your OAuth "
+ "request token. Once that is complete, press any key to "
+ "continue...");
System.in.read();
////////////////////////////////////////////////////////////////////////////
// STEP 4: Get the Access Token
////////////////////////////////////////////////////////////////////////////
// Once the user authorizes with Google, the request token can be exchanged
// for a long-lived access token. If you are building a browser-based
// application, you should parse the incoming request token from the url and
// set it in GoogleOAuthParameters before calling getAccessToken().
String token = oauthHelper.getAccessToken(oauthParameters);
System.out.println("OAuth Access Token: " + token);
System.out.println();
////////////////////////////////////////////////////////////////////////////
// STEP 5: Make an OAuth authorized request to Google
////////////////////////////////////////////////////////////////////////////
// Initialize the variables needed to make the request
URL feedUrl = new URL(variables.getFeedUrl());
System.out.println("Sending request to " + feedUrl.toString());
System.out.println();
GoogleService googleService =
new GoogleService(variables.getGoogleServiceName(), "oauth-sample-app");
// Set the OAuth credentials which were obtained from the step above.
googleService.setOAuthCredentials(oauthParameters, signer);
// Make the request to Google
BaseFeed resultFeed = googleService.getFeed(feedUrl, Feed.class);
System.out.println("Response Data:");
System.out.println("=====================================================");
System.out.println("| TITLE: " + resultFeed.getTitle().getPlainText());
if (resultFeed.getEntries().size() == 0) {
System.out.println("|\tNo entries found.");
} else {
for (int i = 0; i < resultFeed.getEntries().size(); i++) {
BaseEntry entry = (BaseEntry) resultFeed.getEntries().get(i);
System.out.println("|\t" + (i + 1) + ": "
+ entry.getTitle().getPlainText());
}
}
System.out.println("=====================================================");
System.out.println();
////////////////////////////////////////////////////////////////////////////
// STEP 6: Revoke the OAuth token
////////////////////////////////////////////////////////////////////////////
System.out.println("Revoking OAuth Token...");
oauthHelper.revokeToken(oauthParameters);
System.out.println("OAuth Token revoked...");
}
}
@@ -0,0 +1,66 @@
/* Copyright (c) 2008 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 sample.oauth;
import java.util.ArrayList;
/**
* Loads the data related to making an OAuth request. It isn't necessary to
* understand the details of this class in order to understand the OAuth
* examples.
*
*
*/
public class OAuthUserInputHelper extends UserInputHelper {
@Override
public ArrayList<UserInputVariables.GoogleServiceType>
getSupportedServices() {
ArrayList<UserInputVariables.GoogleServiceType> services =
new ArrayList<UserInputVariables.GoogleServiceType>();
services.add(UserInputVariables.GoogleServiceType.Blogger);
services.add(UserInputVariables.GoogleServiceType.Calendar);
services.add(UserInputVariables.GoogleServiceType.Contacts);
services.add(UserInputVariables.GoogleServiceType.Finance);
services.add(UserInputVariables.GoogleServiceType.Picasa);
return services;
}
@Override
public UserInputVariables getVariables() {
UserInputVariables variables = new UserInputVariables();
printHeader();
variables.setGoogleService(getGoogleServiceType());
variables.setConsumerKey(getOAuthConsumerKey());
variables.setSignatureMethod(getSignatureMethod());
String key;
switch (variables.getSignatureMethod()) {
case RSA:
key = getRsaPrivateKey();
break;
case HMAC:
key = getConsumerSecret();
break;
default:
throw new IllegalArgumentException("Invalid Signature Method: "
+ variables.getSignatureMethod().toString());
}
variables.setSignatureKey(key);
return variables;
}
}
@@ -0,0 +1,116 @@
/* Copyright (c) 2008 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 sample.oauth;
import com.google.gdata.client.GoogleService;
import com.google.gdata.client.authn.oauth.GoogleOAuthHelper;
import com.google.gdata.client.authn.oauth.GoogleOAuthParameters;
import com.google.gdata.client.authn.oauth.OAuthHmacSha1Signer;
import com.google.gdata.client.authn.oauth.OAuthSigner;
import com.google.gdata.data.BaseEntry;
import com.google.gdata.data.BaseFeed;
import com.google.gdata.data.Feed;
import java.net.URL;
/**
* Sample application demonstrating how to do 2-Legged OAuth in the Google Data
* Java Client. See the comments below to learn about the details.
*
*
*/
public class TwoLeggedOAuthExample {
public static void main(String[] args) throws Exception {
////////////////////////////////////////////////////////////////////////////
// STEP 1: Gather the user's information
////////////////////////////////////////////////////////////////////////////
// This step collects information from the user, such as the consumer key
// and which service to query. This is just a general setup routine, and
// the method by which you collect user information may be different in your
// implementation.
UserInputHelper inputController =
new TwoLeggedOAuthUserInputHelper();
UserInputVariables variables = inputController.getVariables();
////////////////////////////////////////////////////////////////////////////
// STEP 2: Set up the OAuth objects
////////////////////////////////////////////////////////////////////////////
// You first need to initialize a few OAuth-related objects.
// GoogleOAuthParameters holds all the parameters related to OAuth.
// OAuthSigner is responsible for signing the OAuth base string.
GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
// Set your OAuth Consumer Key (which you can register at
// https://www.google.com/accounts/ManageDomains).
oauthParameters.setOAuthConsumerKey(variables.getConsumerKey());
// Initialize the OAuth Signer. 2-Legged OAuth must use HMAC-SHA1, which
// uses the OAuth Consumer Secret to sign the request. The OAuth Consumer
// Secret can be obtained at https://www.google.com/accounts/ManageDomains.
oauthParameters.setOAuthConsumerSecret(variables.getSignatureKey());
OAuthSigner signer = new OAuthHmacSha1Signer();
// Finally create a new GoogleOAuthHelperObject. This is the object you
// will use for all OAuth-related interaction.
GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(signer);
////////////////////////////////////////////////////////////////////////////
// STEP 3: Make a request to Google
////////////////////////////////////////////////////////////////////////////
// Set the scope for this particular service.
oauthParameters.setScope(variables.getScope());
// Append the "xoauth_requestor_id" parameter to the feed url. This
// parameter indicates which user you are loading the data for.
String feedUrlString = variables.getFeedUrl();
feedUrlString += "?xoauth_requestor_id="
+ variables.getVariable("xoauth_requestor_id");
URL feedUrl = new URL(feedUrlString);
System.out.println("Sending request to " + feedUrl.toString());
System.out.println();
GoogleService googleService =
new GoogleService(variables.getGoogleServiceName(),
"2-legged-oauth-sample-app");
// Set the OAuth credentials which were obtained from the steps above.
googleService.setOAuthCredentials(oauthParameters, signer);
// Make the request to Google
BaseFeed resultFeed = googleService.getFeed(feedUrl, Feed.class);
System.out.println("Response Data:");
System.out.println("=====================================================");
System.out.println("| TITLE: " + resultFeed.getTitle().getPlainText());
if (resultFeed.getEntries().size() == 0) {
System.out.println("|\tNo entries found.");
} else {
for (int i = 0; i < resultFeed.getEntries().size(); i++) {
BaseEntry entry = (BaseEntry) resultFeed.getEntries().get(i);
System.out.println("|\t" + (i + 1) + ": "
+ entry.getTitle().getPlainText());
}
}
System.out.println("=====================================================");
}
}
@@ -0,0 +1,54 @@
/* Copyright (c) 2008 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 sample.oauth;
import java.util.ArrayList;
/**
* Loads the data related to making a 2-Legged OAuth request. It isn't
* necessary to understand the details of this class in order to understand the
* OAuth examples.
*
*
*/
public class TwoLeggedOAuthUserInputHelper extends UserInputHelper {
@Override
public ArrayList<UserInputVariables.GoogleServiceType>
getSupportedServices() {
ArrayList<UserInputVariables.GoogleServiceType> services =
new ArrayList<UserInputVariables.GoogleServiceType>();
services.add(UserInputVariables.GoogleServiceType.Calendar);
services.add(UserInputVariables.GoogleServiceType.Contacts);
return services;
}
@Override
public UserInputVariables getVariables() {
UserInputVariables variables = new UserInputVariables();
printHeader();
variables.setGoogleService(getGoogleServiceType());
variables.setConsumerKey(getOAuthConsumerKey());
variables.setSignatureMethod(UserInputVariables.SignatureMethod.HMAC);
variables.setSignatureKey(getConsumerSecret());
System.out.println("Enter the full email address of the user who's data you"
+ " would like to load (for example, username@domain.com)");
variables.setVariable("xoauth_requestor_id", readCommandLineInput());
return variables;
}
}
@@ -0,0 +1,112 @@
/* Copyright (c) 2008 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 sample.oauth;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
/**
* Helper class for loading data related to making an OAuth request. It isn't
* necessary to understand the details of this class in order to understand the
* OAuth examples.
*
*
*/
public abstract class UserInputHelper {
/** Helper method to read input from the command line. */
protected static String readCommandLineInput() {
System.out.print("> ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String userInput = null;
try {
userInput = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read input!");
System.exit(1);
}
System.out.println();
return userInput;
}
/** Loads the variables from the user for a specific OAuth request. */
public abstract UserInputVariables getVariables();
/** Loads the services that a supported by this specific example. */
protected abstract ArrayList<UserInputVariables.GoogleServiceType>
getSupportedServices();
/** Print out the header that begins each OAuth example. */
protected void printHeader() {
System.out.println();
System.out.println("=============");
System.out.println("Testing OAuth");
System.out.println("=============");
System.out.println();
System.out.println("This sample will show you how to use OAuth to retrieve "
+ "information from a Google Data service. Follow the instructions "
+ "below to continue");
System.out.println();
}
/** Get the Google service type from the user. */
protected UserInputVariables.GoogleServiceType getGoogleServiceType() {
System.out.println("Please select a Google service to query:");
ArrayList<UserInputVariables.GoogleServiceType> services =
getSupportedServices();
for (int i = 0; i < services.size(); i++) {
System.out.println("\t" + (i + 1) + ") " + services.get(i).toString());
}
return services.get(Integer.parseInt(readCommandLineInput()) - 1);
}
/** Get the signature method from the user. */
protected UserInputVariables.SignatureMethod getSignatureMethod() {
System.out.println("Please select a signature method:");
for (UserInputVariables.SignatureMethod m :
UserInputVariables.SignatureMethod.values()) {
System.out.println("\t" + (m.ordinal() + 1) + ") " + m.toString());
}
return UserInputVariables.SignatureMethod.values()[
Integer.parseInt(readCommandLineInput()) - 1];
}
/** Get the consumer key from the user. */
protected String getOAuthConsumerKey() {
System.out.println("Please enter your OAuth consumer key (usually your "
+ "domain, visit https://www.google.com/accounts/ManageDomains to "
+ "manage your OAuth parameters)");
return readCommandLineInput();
}
/** Get the RSA private key from the user. */
protected String getRsaPrivateKey() {
System.out.println("Please enter your RSA private key (the key should "
+ "be a Base-64 encoded string conforming to the PKCS #8 standard");
return readCommandLineInput();
}
/** Get the consumer secret from the user. */
protected String getConsumerSecret() {
System.out.println("Please enter your OAuth consumer secret (visit "
+ "https://www.google.com/accounts/ManageDomains to view your "
+ "consumer secret)");
return readCommandLineInput();
}
}
@@ -0,0 +1,160 @@
/* Copyright (c) 2008 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 sample.oauth;
import java.util.HashMap;
import java.util.Map;
/**
* Container for all user input variables related to the OAuth examples. It
* isn't necessary to understand the details of this class in order to
* understand the OAuth examples.
*
*
*/
public class UserInputVariables {
/** The various Google services enabled in this sample. */
public enum GoogleServiceType {
Blogger,
Calendar,
Contacts,
Finance,
Picasa
}
/** The signature methods supported by OAuth in the Java client. */
public enum SignatureMethod {
HMAC,
RSA
}
/** The service the user is accessing. */
private GoogleServiceType serviceType;
/** The authentication scope of the request. */
private String scope;
/** The feed url of the request. */
private String feedUrl;
/** The Google service name for the request. */
private String googleServiceName;
/** The signature method. */
private SignatureMethod signatureMethod;
/**
* The key to use when signing the request. This is either the private key
* when {@link #signatureMethod} equals RSA, or the consumer secret when
* {@link #signatureMethod} equals HMAC.
*/
private String signatureKey;
/** The OAuth consumer key. */
private String consumerKey;
/** A storage area for other example-specific variables. */
private Map<String, String> otherVars = new HashMap<String, String>();
public String getGoogleServiceName() {
return googleServiceName;
}
public String getFeedUrl() {
return feedUrl;
}
public String getScope() {
return scope;
}
public GoogleServiceType getServiceType() {
return serviceType;
}
public void setConsumerKey(String key) {
consumerKey = key;
}
public String getConsumerKey() {
return consumerKey;
}
public void setSignatureKey(String key) {
this.signatureKey = key;
}
public String getSignatureKey() {
return signatureKey;
}
public void setSignatureMethod(SignatureMethod signatureMethod) {
this.signatureMethod = signatureMethod;
}
public SignatureMethod getSignatureMethod() {
return signatureMethod;
}
public void setVariable(String key, String value) {
otherVars.put(key, value);
}
public String getVariable(String key) {
return otherVars.get(key);
}
/**
* Sets the {@link #scope}, {@link #feedUrl} and {@link #googleServiceName}
* for a given {@link GoogleServiceType}.
*/
public void setGoogleService(GoogleServiceType stype) {
this.serviceType = stype;
switch (stype) {
case Contacts:
scope = "http://www.google.com/m8/feeds/";
feedUrl = "http://www.google.com/m8/feeds/contacts/default/base";
googleServiceName = "cp";
break;
case Calendar:
scope = "http://www.google.com/calendar/feeds/";
feedUrl =
"http://www.google.com/calendar/feeds/default/allcalendars/full";
googleServiceName = "cl";
break;
case Blogger:
scope = "http://www.blogger.com/feeds/";
feedUrl = "http://www.blogger.com/feeds/default/blogs";
googleServiceName = "blogger";
break;
case Finance:
scope = "http://finance.google.com/finance/feeds/";
feedUrl =
"http://finance.google.com/finance/feeds/default/portfolios";
googleServiceName = "finance";
break;
case Picasa:
scope = "http://picasaweb.google.com/data/";
feedUrl = "http://picasaweb.google.com/data/feed/api/user/default";
googleServiceName = "lh2";
break;
default:
throw new IllegalArgumentException("Unsupported Google Service");
}
}
}
Binary file not shown.