Android Sina Weibo Certification

Source: Internet
Author: User
Tags oauth

Sina Weibo has been very tangled, and Auth authentication is a sign of blood. Previously, the project was used by others. Recently I want to know more about it, so that I can use it better in the future.

Be sure to keep the following Constants

Public String consumerkey = "2803926882"; // key
Public String consumersecret = "d41e6f2d605bd16c785879032ec431f5"; // secret
// Customize the callback URL (this URL must be configured in the configuration file ,)
Public static final string callback_url = "jjhappyforever: // weibolistactivity ";

// Requesttokenurl
Private final string requesttokenendpointurl = "http://api.t.sina.com.cn/oauth/request_token ";
// Accesstokenurl
Private final string accesstokenendpointurl = "http://api.t.sina.com.cn/oauth/access_token ";
// Authorization
Private final string authorizationwebsiteurl = "http://api.t.sina.com.cn/oauth/authorize ";

The following two methods are used in oauth. java.

// Obtain the auth address
Public String retrieveauthurl () throws oauthmessagesignerexception,
Oauthnotauthorizedexception, oauthexpectationfailedexception,
Oauthcommunicationexception {
Authconsumer = new commonshttpoauthconsumer (consumerkey, consumersecret );
Authprovider = new commonshttpoauthprovider (requesttokenendpointurl,
Accesstokenendpointurl, authorizationwebsiteurl );
Return authprovider. retrieverequesttoken (authconsumer, callback_url );
}

// This method is used to obtain the user's userid, Token, tokensecret, and other information.

Public user getaccesstoken (string authurl)
Throws oauthmessagesignerexception, oauthnotauthorizedexception,
Oauthexpectationfailedexception, oauthcommunicationexception {

Log. I (TAG, authurl );

Authprovider. setoauth10a (true );
Authprovider. retrieveaccesstoken (authconsumer, authurl );
User user = new user ();
User. userid = authprovider. getresponseparameters (). getfirst ("user_id ");
User. Token = authconsumer. gettoken ();
User. tokensecret = authconsumer. gettokensecret ();
Return user;
}

Called in activity.

// Obtain the authurl address
String authurl = oauth. retrieveauthurl ();
// Start accessing the Sina Certification Page
Startactivity (new intent (intent. action_view, Uri. parse (authurl )));

In this way, Web authentication is performed.

The onnewintent method is implemented in this activity to execute the action after the authentication callback,

Protected void onnewintent (intent ){
Super. onnewintent (intent );
Uri uri = intent. getdata ();
String verifier = URI. getqueryparameter (oauth. oauth_verifier );
Try {
User user = NULL;
User = oauth. getaccesstoken (verifier );
Log. I (oauth. Tag, user. tostring ());
} Catch (oauthmessagesignerexception ex ){
Ex. printstacktrace ();
} Catch (oauthnotauthorizedexception ex ){
Ex. printstacktrace ();
} Catch (oauthexpectationfailedexception ex ){
Ex. printstacktrace ();
} Catch (oauthcommunicationexception ex ){
Ex. printstacktrace ();
}
}

 

Here we will talk about the configuration in the configuration file:

<Activity
Android: Name = ". testactivity"
Android: launchmode = "singleinstance"> <! --- This lanchmode mode must be set. If this mode is not set, oncreate will be executed all the time. onnewintent will not be executed. I am stuck here for a long time. -->
<Intent-filter>
<Action Android: Name = "android. Intent. Action. View"/>

<Category Android: Name = "android. Intent. Category. Default"/>
<Category Android: Name = "android. Intent. Category. browsable"/>

<Data
Android: host = "oauth"
Android: Scheme = "jjhappyforevert"/>
</Intent-filter>
</Activity>

 

The oauth authentication process is implemented here. Next, we will briefly introduce the Weibo information that can be called by users based on open platform APIs.

 

 

 

 

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.