Advertisement
Guest User

Jayesh Salvi

a guest
Mar 23rd, 2010
2,048
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.56 KB | None | 0 0
  1. /*
  2.  * Google Reader OAuth sample code.
  3.  *
  4.  * Copyright (C) 2010 Jayesh Salvi (http://www.altcanvas.com)
  5.  *
  6.  * Licensed under the Apache License, Version 2.0 (the "License");
  7.  * you may not use this file except in compliance with the License.
  8.  * You may obtain a copy of the License at
  9.  *
  10.  *      http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing, software
  13.  * distributed under the License is distributed on an "AS IS" BASIS,
  14.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15.  * See the License for the specific language governing permissions and
  16.  * limitations under the License.
  17.  *
  18.  * INSTRUCTIONS:
  19.  *
  20.  * To get consumer key and secret for your app check out:
  21.  * http://code.google.com/apis/accounts/docs/RegistrationForWebAppsAuto.html
  22.  *
  23.  * Required libraries:
  24.  * Signpost (OAuth implementation library):
  25.  *  http://oauth-signpost.googlecode.com/files/signpost-core-1.2.1.1.jar
  26.  * Apache Codec (requirement of Signpost) :
  27.  *  http://download.filehat.com/apache/commons/codec/binaries/commons-codec-1.4-bin.tar.gz
  28.  *
  29.  *  Build instructions:
  30.  *  javac -classpath=<path-of-signpost-core.jar> GReaderSignPost.java
  31.  *
  32.  *  Run instructions:
  33.  *  java -classpath=.:<path-of-signpost-core.jar>:<path-of-commons-codec.jar> GReaderSignPost
  34.  */
  35.  
  36. import java.io.*;
  37. import java.net.*;
  38. import oauth.signpost.*;
  39. import oauth.signpost.basic.*;
  40. import oauth.signpost.exception.OAuthMessageSignerException;
  41. import oauth.signpost.exception.OAuthNotAuthorizedException;
  42.  
  43. public class GReaderSignPost {
  44.  
  45.     public GReaderSignPost() {};
  46.  
  47.     private static final String CONSUMER_KEY = "<YOUR KEY>";
  48.     private static final String CONSUMER_SECRET = "<YOUR SECRET>";
  49.     private static final String APPNAME = "ReaderScope";
  50.     private static final String scope =
  51.         "http://www.google.com/reader/api";
  52.     public static final String reqtokenURL =
  53.         "https://www.google.com/accounts/OAuthGetRequestToken";
  54.     public static final String authorizeURL =
  55.         "https://www.google.com/accounts/OAuthAuthorizeToken";
  56.     public static final String accessTokenURL =
  57.         "https://www.google.com/accounts/OAuthGetAccessToken";
  58.  
  59.     private static String accessToken = null;
  60.     private static String tokenSecret = null;
  61.  
  62.     public static void main(String[] args)
  63.     {
  64.         try {
  65.             OAuthConsumer oac = new DefaultOAuthConsumer(
  66.                 CONSUMER_KEY, CONSUMER_SECRET);
  67.  
  68.             OAuthProvider oap = new DefaultOAuthProvider(
  69.                 reqtokenURL+"?scope="+scope+"&"+"xoauth_displayname="+APPNAME,
  70.                 accessTokenURL,
  71.                 authorizeURL+"?hl=en&btmpl=mobile");
  72.             String url = oap.retrieveRequestToken(oac, OAuth.OUT_OF_BAND);
  73.             System.out.println("Open this URL in browser and Grant Access to "+
  74.                             APPNAME+": "+url);
  75.             System.out.print("Enter Verification code: ");
  76.             BufferedReader ibr =
  77.                 new BufferedReader(new InputStreamReader(System.in));
  78.             String verifCode = ibr.readLine();
  79.  
  80.             oap.retrieveAccessToken(oac, verifCode);
  81.             accessToken = oac.getToken();
  82.             tokenSecret = oac.getTokenSecret();
  83.  
  84.             getSubscriptionList();
  85.         } catch(Exception e) {
  86.             System.out.println(e.toString());
  87.         }
  88.     }
  89.  
  90.     public static void getSubscriptionList() throws Exception
  91.     {
  92.         OAuthConsumer consumer = new DefaultOAuthConsumer(
  93.                 CONSUMER_KEY, CONSUMER_SECRET);
  94.         consumer.setTokenWithSecret(accessToken, tokenSecret);
  95.         String url =
  96.             "http://www.google.com/reader/api/0/subscription/list?output=json";
  97.         HttpURLConnection conn =
  98.             (HttpURLConnection) new URL(url).openConnection();
  99.  
  100.         conn.setRequestMethod("GET");
  101.         conn.setConnectTimeout(10*1000);
  102.         conn.setReadTimeout(25*1000);
  103.  
  104.         consumer.sign(conn);
  105.  
  106.         conn.connect();
  107.  
  108.         if(conn.getResponseCode() != 200) {
  109.             System.out.println("Error: "+
  110.                 conn.getResponseCode()+" "+conn.getResponseMessage());
  111.             return;
  112.         }
  113.  
  114.         InputStream is = conn.getInputStream();
  115.  
  116.         BufferedReader br =
  117.             new BufferedReader(new InputStreamReader(is, "UTF-8"));
  118.         StringBuffer buf = new StringBuffer();
  119.         String line;
  120.         while (null != (line = br.readLine())) {
  121.             buf.append(line).append("\n");
  122.         }
  123.         System.out.println(buf.toString());
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement