Android WeChat third-party login (personal notes), android third-party

Source: Internet
Author: User
Tags openid

Android third-party Logon (personal notes), android third-party

I am writing a login today. It took me half a day to get it done, and then I wrote my own notes, hoping to help more people... Thank you for your advice.

 

Authorized logon. What the official saying is not very clear, which leads to some pitfalls.

Register the application signature of the application platform. Download the signature generation tool and enter the packageName of the project.

(Note: debug and Release must be differentiated. Because the two types of generated signatures are inconsistent, no jump or callback occurs .... Generally, the developer registers the formal environment package ,)

 

 

 

The application for a developer account or application is omitted ....

 

 

1. AndroidManifest. xml permission

<! -- Permission -->
<Uses-permission android: name = "android. permission. INTERNET"/> <uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"/>
<Uses-permission android: name = "android. permission. ACCESS_WIFI_STATE"/>
<Uses-permission android: name = "android. permission. READ_PHONE_STATE"/>
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>

 

 

2. In Figure 1, add the jump method, that is, the login start method.

Private void logtailweixin () {IWXAPI mApi = response. createWXAPI (this, WXEntryActivity. WEIXIN_APP_ID, true); mApi. registerApp (WXEntryActivity. WEIXIN_APP_ID); if (mApi! = Null & mApi. isWXAppInstalled () {SendAuth. req req = new SendAuth. req (); req. scope = "snsapi_userinfo"; req. state = "wechat_sdk_demo_test_neng"; mApi. sendReq (req);} else Toast. makeText (this, "not installed by the user", Toast. LENGTH_SHORT ). show ();}

 

3. Create a wxapi directory under the corresponding directory of your package name, and add a WXEntryActivity class under the wxapi Directory, which inherits from the Activity

Package (project registration). wxapi. WXEntryActivity. Java

The onResp () method of the callback method does not trigger the callback. Please check whether mApi. handleIntent (this. getIntent (), this) is called in WXEntryActivity onCreate );

Package com. xxx. xxxx. android. wxapi; /*** logon page * @ author kevin_chen 19:03:45 * @ version v1.0 */public class WXEntryActivity extends Activity implements IWXAPIEventHandler {private static final String APP_SECRET = "fill in your AppSecret "; private IWXAPI mWeixinAPI; public static final String WEIXIN_APP_ID = "Enter your APP_id"; private static String uuid; @ Override public void onCreate (Bundle savedI NstanceState) {super. onCreate (savedInstanceState); mWeixinAPI = wxapifacloud. createWXAPI (this, WEIXIN_APP_ID, true); mWeixinAPI. handleIntent (this. getIntent (), this) ;}@ Override protected void onNewIntent (Intent intent) {super. onNewIntent (intent); setIntent (intent); mWeixinAPI. handleIntent (intent, this); // this sentence must be called} // the request sent will be called back to the onReq method @ Override public void onReq (BaseReq req) {LogUtils. log ("onReq ");} // Response result sent to the request @ Override public void onResp (BaseResp resp) {LogUtils. log ("onResp"); switch (resp. errCode) {case BaseResp. errCode. ERR_ OK: LogUtils. log ("ERR_ OK"); // SendAuth is successfully sent. resp sendResp = (SendAuth. resp) resp; if (sendResp! = Null) {String code = sendResp. code; getAccess_token (code);} break; case BaseResp. errCode. ERR_USER_CANCEL: LogUtils. log ("ERR_USER_CANCEL"); // cancel sending break; case BaseResp. errCode. ERR_AUTH_DENIED: LogUtils. log ("ERR_AUTH_DENIED"); // The break is rejected for sending; default: // The break is returned for sending ;}} /*** obtain the openid accessToken value for subsequent operations * @ param code Request code */private void getAccess_token (final String code) {String path = "https: // ap I .weixin.qq.com/sns/oauth2/access_token? Appid = "+ WEIXIN_APP_ID +" & secret = "+ APP_SECRET +" & code = "+ code +" & grant_type = authorization_code "; LogUtils. log ("getAccess_token:" + path); // network request, based on your own request method VolleyRequest. get (this, path, "getAccess_token", false, null, new VolleyRequest. callback () {@ Override public void onSuccess (String result) {LogUtils. log ("getAccess_token_result:" + result); JSONObject jsonObject = null; try {jsonObject = New JSONObject (result); String openid = jsonObject. getString ("openid "). toString (). trim (); String access_token = jsonObject. getString ("access_token "). toString (). trim (); getUserMesg (access_token, openid);} catch (JSONException e) {e. printStackTrace () ;}@ Override public void onError (String errorMessage ){}});} /*** obtain personal information * @ param access_token * @ param openid */private void getUserMesg (f Inal String access_token, final String openid) {String path = "https://api.weixin.qq.com/sns/userinfo? Access_token = "+ access_token +" & openid = "+ openid; LogUtils. log ("getUserMesg:" + path); // network request, based on your own request method VolleyRequest. get (this, path, "getAccess_token", false, null, new VolleyRequest. callback () {@ Override public void onSuccess (String result) {LogUtils. log ("getUserMesg_result:" + result); JSONObject jsonObject = null; try {jsonObject = new JSONObject (result); String nickname = jsonObject. getString ("nickname"); int sex = Integer. parseInt (jsonObject. get ("sex "). toString (); String headimgurl = jsonObject. getString ("headimgurl"); LogUtils. log ("basic user information:"); LogUtils. log ("nickname:" + nickname); LogUtils. log ("sex:" + sex); LogUtils. log ("headimgurl:" + headimgurl);} catch (JSONException e) {e. printStackTrace () ;}finish () ;}@ Override public void onError (String errorMessage ){}});}}

 

5. Set the registration page in AndroidManifest. xml

When authorizing WXEntryActivity, the current background may be plain or white. To avoid affecting the user experience, you can set the current Activity to transparent:

Android: theme = "@ android: style/Theme. Translucent"

<! -- Register the callback WXEntryActivity --> <activity android: name = ". wxapi. WXEntryActivity "android: exported =" true "android: label =" @ string/app_name "android: launchMode =" singleTop "android: theme =" @ android: style/Theme. translucent "/>

 

 

 

 

For details, refer to the steps for authorizing logon to retrieve user information.

 

Related Article

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.