Google oauth 1.0 standalone app example

Source: Internet
Author: User
Tags hmac oauth

!!! Oauth in the Google Data Protocol client libraries Description: http://code.google.com/intl/zh-TW/apis/gdata/docs/auth/oauth.html

Package example_tomson.oauth1; import java.net. URL; 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;/*** Google oauth 1.0 see the http://blog.csdn.net/totogogo/article/details/6835820 *** this example is a demo of Google oauth 1.0, it is for standalone application, that is, there is no callback_url parameter. If * callback URL parameter is not set, it is regarded as anonymous. After the user clicks the authorize button, a Google handle result MSG web page ** will appear. If the web app uses oauth 1.0, the callback URL will be set, after the user clicks the authorize button, * redirect to callback URL with authorized token and related Params ** when the standalone app uses auth 1.0, it also needs register a domain to obtain the consumer key and secret. * **/public class oauth1example {public static void main (string [] ARGs) throws exception {// The comsumer key is St Ring oauthconsumerkey = "chtl.hkbu.edu.hk"; string oauthconsumersecret = "xxxxxx"; string signaturemethod = "HMAC"; // HMAC or RSA // For calendar apistring scope = "http://www.google.com/calendar/feeds "; string strcalendarfeedurl = "http://www.google.com/calendar/feeds/default/allcalendars/full "; //////////////////////////////////////// /// // Step 1: 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. // create a googleoauthparameters instance, which contains all oauth parametergoogleoauthparameters oauthparameters = new googleoauthparameters (); oauthparamete Rs. setoauthconsumerkey (oauthconsumerkey); oauthparameters. setscope (scope); // 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 o Auth consumer secret, which can be obtained at // https://www.google.com/accounts/ManageDomains. // create an oauth signer instance oauthsigner signer = NULL; If (signaturemethod = "HMAC") {// if you use the HMAC-SHA1 method, you need to provide oauth consumer secret in googleoauthparameters OBJ (you do not need to provide it using RSA-SHA1 method) oauthparameters. setoauthconsumersecret (oauthconsumersecret); signer = new oauthhmacsha1signer ();} else if (signaturemethod = "R SA ") {// If RSA-SHA1 method is used, you must provide your PKCS #8 standard private key as a base-64 string signaturekey = "<RSA private key>"; signer = new oauthrsasha1signer (signaturekey );} //////////////////////////////////////// /// // Step 2: get the authorization URL //////////////////////////////////// //////////////////////////////////////// // create a new googleoauthhelperobject. This is the object you will use for all oauth-related interaction. // create googleoauthhelper OBJ with oauth signer OBJ as the parameter, and then use googleoauthparameters as the call authorization method parameter to obtain unauthorized request token googleoauthhelper oauthhelper = new googleoauthhelper (signer); oauthhelper. getunauthorizedrequesttoken (oauthparameters); // send a request to get the unauthorized request token //!!! Then it will write the unauthorized request token and Token secret returned by response into the oauthparameters object // system. out. println ("========================================== ==================== "); // system. out. println ("auth token is:" + oauthparameters. getoauthtoken (); // system. out. println ("auth token secret is:" + oauthparameters. getoauthtokensecret (); // system. out. println ("auth signature is:" + oauthparameters. getoauthsignature (); // System. out. println ("auth signature method is:" + oauthparameters. getoauthsignaturemethod (); // system. out. println ("auth nonce is:" + oauthparameters. getoauthnonce (); // system. out. println ("auth verifier is:" + oauthparameters. getoauthverifier (); // system. out. println ("========================================== ==================== "); // get the authorization URL. the user of your application must v Isit // 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. // call createuserauthorizationurl method to generate a URL for the user to authorize oauth request token. // The user can access to this URL in the browser, click authorization button to allow the app to access your Google account to obtain data/INFO/file string requesturl = oauthhelper. createuserauthorizationur L (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 3: 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 (). //!! Note: In the previous step, the user does not need to obtain any returned parameter value from the browser after authorization. (You only need to authorize the request token ), then you can call // oauthhelper. getaccesstoken (oauthparameters) // to obtain the access token ,//!!! At the same time, the oauth token value in oauthparameters will be converted from authorized request token to access token !!! String token = oauthhelper. getaccesstoken (oauthparameters); system. out. println ("oauth access token:" + token ); //////////////////////////////////////// /// // Step 4: access calendar API ///////////////////////////////////// /// // URL feedurl = new URL (strcalendarfeedurl ); system. out. println ("sending request to" + feedurl. tostring (); System. out. println (); string googleservicename = "Cl"; googleservice = new googleservice (googleservicename, "oauth-Sample-app "); // set the oauth credentials which were obtained from the step above. // The parameter oauthparameters contains the access token info googleservice obtained from the previous step. setoauthcredentials (oauthparameters, signer );//!!! Key Method: 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 5: revoke the oauth token //////////////////////////////////// //////////////////////////////////////// system. out. println ("revoking oauth token... "); oauthhelper. revoketoken (oauthparameters); system. out. println ("oauth token revoked... ");}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.