/java/sample/oauth/TwoLeggedOAuthExample.java

http://gdata-java-client.googlecode.com/ · Java · 116 lines · 47 code · 21 blank · 48 comment · 4 complexity · c30ed5d2b0ef5df4420df0c57f3fcc82 MD5 · raw file

  1. /* Copyright (c) 2008 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. package sample.oauth;
  16. import com.google.gdata.client.GoogleService;
  17. import com.google.gdata.client.authn.oauth.GoogleOAuthHelper;
  18. import com.google.gdata.client.authn.oauth.GoogleOAuthParameters;
  19. import com.google.gdata.client.authn.oauth.OAuthHmacSha1Signer;
  20. import com.google.gdata.client.authn.oauth.OAuthSigner;
  21. import com.google.gdata.data.BaseEntry;
  22. import com.google.gdata.data.BaseFeed;
  23. import com.google.gdata.data.Feed;
  24. import java.net.URL;
  25. /**
  26. * Sample application demonstrating how to do 2-Legged OAuth in the Google Data
  27. * Java Client. See the comments below to learn about the details.
  28. *
  29. *
  30. */
  31. public class TwoLeggedOAuthExample {
  32. public static void main(String[] args) throws Exception {
  33. ////////////////////////////////////////////////////////////////////////////
  34. // STEP 1: Gather the user's information
  35. ////////////////////////////////////////////////////////////////////////////
  36. // This step collects information from the user, such as the consumer key
  37. // and which service to query. This is just a general setup routine, and
  38. // the method by which you collect user information may be different in your
  39. // implementation.
  40. UserInputHelper inputController =
  41. new TwoLeggedOAuthUserInputHelper();
  42. UserInputVariables variables = inputController.getVariables();
  43. ////////////////////////////////////////////////////////////////////////////
  44. // STEP 2: Set up the OAuth objects
  45. ////////////////////////////////////////////////////////////////////////////
  46. // You first need to initialize a few OAuth-related objects.
  47. // GoogleOAuthParameters holds all the parameters related to OAuth.
  48. // OAuthSigner is responsible for signing the OAuth base string.
  49. GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
  50. // Set your OAuth Consumer Key (which you can register at
  51. // https://www.google.com/accounts/ManageDomains).
  52. oauthParameters.setOAuthConsumerKey(variables.getConsumerKey());
  53. // Initialize the OAuth Signer. 2-Legged OAuth must use HMAC-SHA1, which
  54. // uses the OAuth Consumer Secret to sign the request. The OAuth Consumer
  55. // Secret can be obtained at https://www.google.com/accounts/ManageDomains.
  56. oauthParameters.setOAuthConsumerSecret(variables.getSignatureKey());
  57. OAuthSigner signer = new OAuthHmacSha1Signer();
  58. // Finally create a new GoogleOAuthHelperObject. This is the object you
  59. // will use for all OAuth-related interaction.
  60. GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(signer);
  61. ////////////////////////////////////////////////////////////////////////////
  62. // STEP 3: Make a request to Google
  63. ////////////////////////////////////////////////////////////////////////////
  64. // Set the scope for this particular service.
  65. oauthParameters.setScope(variables.getScope());
  66. // Append the "xoauth_requestor_id" parameter to the feed url. This
  67. // parameter indicates which user you are loading the data for.
  68. String feedUrlString = variables.getFeedUrl();
  69. feedUrlString += "?xoauth_requestor_id="
  70. + variables.getVariable("xoauth_requestor_id");
  71. URL feedUrl = new URL(feedUrlString);
  72. System.out.println("Sending request to " + feedUrl.toString());
  73. System.out.println();
  74. GoogleService googleService =
  75. new GoogleService(variables.getGoogleServiceName(),
  76. "2-legged-oauth-sample-app");
  77. // Set the OAuth credentials which were obtained from the steps above.
  78. googleService.setOAuthCredentials(oauthParameters, signer);
  79. // Make the request to Google
  80. BaseFeed resultFeed = googleService.getFeed(feedUrl, Feed.class);
  81. System.out.println("Response Data:");
  82. System.out.println("=====================================================");
  83. System.out.println("| TITLE: " + resultFeed.getTitle().getPlainText());
  84. if (resultFeed.getEntries().size() == 0) {
  85. System.out.println("|\tNo entries found.");
  86. } else {
  87. for (int i = 0; i < resultFeed.getEntries().size(); i++) {
  88. BaseEntry entry = (BaseEntry) resultFeed.getEntries().get(i);
  89. System.out.println("|\t" + (i + 1) + ": "
  90. + entry.getTitle().getPlainText());
  91. }
  92. }
  93. System.out.println("=====================================================");
  94. }
  95. }