1 2 3 4 | OAuthParameters para = new OAuthParameters(); para.setOAuthConsumerKey("www.example.com"); para.setOAuthToken(accessToken); googleService.setOAuthCredentials(para, signer); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | import java.util.Collection; import java.util.Map; import net.oauth.OAuth; import net.oauth.OAuthAccessor; import net.oauth.OAuthConsumer; import net.oauth.client.OAuthClient; public class DesktopClient { private final OAuthAccessor accessor; private OAuthClient oauthClient = null; public DesktopClient(OAuthConsumer consumer) { accessor = new OAuthAccessor(consumer); } public OAuthClient getOAuthClient() { return oauthClient; } public void setOAuthClient(OAuthClient client) { this.oauthClient = client; } //get the OAuth access token. public String getAccessToken(String httpMethod, Collection<? extends Map.Entry> parameters) throws Exception { getOAuthClient().getRequestTokenResponse(accessor, null,parameters); String authorizationURL = OAuth.addParameters( accessor.consumer.serviceProvider.userAuthorizationURL, OAuth.OAUTH_TOKEN, accessor.requestToken); //Launch the browser and redirects user to authorization URL Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + authorizationURL); //wait for user's authorization System.out.println("Please authorize your OAuth request token. " + "Once that is complete, press any key to continue..."); System.in.read(); oauthClient.getAccessToken(accessor, null, null); return accessor.accessToken; } } import java.net.URL; import java.security.KeyFactory; import java.security.PrivateKey; import java.security.spec.EncodedKeySpec; import java.security.spec.PKCS8EncodedKeySpec; import java.util.Collection; import java.util.Map; import com.google.gdata.client.GoogleService; import com.google.gdata.client.authn.oauth.OAuthParameters; 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 net.oauth.OAuth; import net.oauth.OAuthConsumer; import net.oauth.OAuthMessage; import net.oauth.OAuthServiceProvider; import net.oauth.client.OAuthClient; import net.oauth.client.httpclient4.HttpClient4; import net.oauth.example.desktop.MyGoogleService; import net.oauth.signature.OAuthSignatureMethod; import net.oauth.signature.RSA_SHA1; public class GoogleOAuthExample { //Note, use the private key of your self-signed X509 certificate. private static final String PRIVATE_KEY = "XXXXXXXX"; public static void main(String[] args) throws Exception { KeyFactory fac = KeyFactory.getInstance("RSA"); //PRIVATE_KEY is the private key of your self-signed X509 certificate. EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec( OAuthSignatureMethod.decodeBase64(PRIVATE_KEY)); fac = KeyFactory.getInstance("RSA"); PrivateKey privateKey = fac.generatePrivate(privKeySpec); OAuthServiceProvider serviceProvider = new OAuthServiceProvider( //used for obtaining a request token //"https://www.google.com/accounts/OAuthGetRequestToken", //used for authorizing the request token "https://www.google.com/accounts/OAuthAuthorizeToken", //used for upgrading to an access token "https://www.google.com/accounts/OAuthGetAccessToken"); OAuthConsumer oauthConsumer = new OAuthConsumer(null , "lszhy.weebly.com" //consumer key , "hIsGnM+T4+86fKNesUtJq7Gs" //consumer secret , serviceProvider); oauthConsumer.setProperty(OAuth.OAUTH_SIGNATURE_METHOD, OAuth.RSA_SHA1); oauthConsumer.setProperty(RSA_SHA1.PRIVATE_KEY, privateKey); DesktopClient client = new DesktopClient(oauthConsumer); client.setOAuthClient(new OAuthClient(new HttpClient4())); Collection<? extends Map.Entry> parameters = OAuth.newList("scope","http://www.google.com/calendar/feeds/"); String accessToken = client.getAccessToken(OAuthMessage.GET,parameters); //Make an OAuth authorized request to Google // Initialize the variables needed to make the request URL feedUrl = new URL( "http://www.google.com/calendar/feeds/default/allcalendars/full"); System.out.println("Sending request to " + feedUrl.toString()); System.out.println(); GoogleService googleService = new GoogleService("cl", "oauth-sample-app"); OAuthSigner signer = new OAuthRsaSha1Signer(MyGoogleService.PRIVATE_KEY); // Set the OAuth credentials which were obtained from the step above. OAuthParameters para = new OAuthParameters(); para.setOAuthConsumerKey("lszhy.weebly.com"); para.setOAuthToken(accessToken); googleService.setOAuthCredentials(para, 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("=========================================="); } } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |