Android Micro-trust third party login (personal note) _android

Source: Internet
Author: User
Tags openid

Today, I am writing a micro-letter login, spent half a day to finish, and then write down their notes, hoping to help more people. You are welcome to advise.

Micro-letter authorization to log in, the official said is not very clear, so lead to a part of the pit.

Application of micro-credit registration application platform signature, download the micro-signature generation tool input items PackageName can also be viewed.

( note : Debug, Release must be differentiated, because 2 kinds of generated micro-letter signature inconsistent, will cause no jump, callback ...) Typically registered with a micro-trust developer is a formal environment packaged,

Application for micro-credit developer account, application applications omitted.

1. Androidmanifest.xml Permissions

<!--micro--->
<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. Above the figure one button, plus the micro-letter jump method, which is to start the micro-letter login method

private void Logintoweixin () {
  Iwxapi MApi = Wxapifactory.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, "User not installed micro-letter", Toast.length_short). Show ();
 

3. Create a new Wxapi directory in the appropriate directory for your package name and add a wxentryactivity class to the Wxapi directory , which inherits from the activity

Package (Project registration). Wxapi. Wxentryactivity.java

The callback method Onresp () method does not trigger a callback, please monitor wxentryactivity OnCreate to call this method Mapi.handleintent (This.getintent (), this);

Package Com.xxx.xxxx.android.wxapi; /** * Micro-Letter Login Page * @author kevin_chen 2016-12-10 pm 19:03:45 * @version v1.0 * * public class Wxentryactivity extends Activi
 Ty implements Iwxapieventhandler {private static final String App_secret = "Fill in your own Appsecret";
 Private Iwxapi Mweixinapi;
 public static final String weixin_app_id = "Fill in your own app_id";


 private static String uuid;
  @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Mweixinapi = Wxapifactory.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)//must call this phrase}//micro-letter sent request will callback to the Onreq method @Override public void Onreq (Basereq req) {L
 OgUtils.log ("Onreq");
  The response result sent to the micro-letter request @Override public void Onresp (Baseresp resp) {LogUtils.log ("Onresp");
 Switch (resp.errcode) {case BaseResp.ErrCode.ERR_OK:  LogUtils.log ("Err_ok");
   Send success Sendauth.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");
  Send Cancel break;
   Case BaseResp.ErrCode.ERR_AUTH_DENIED:LogUtils.log ("err_auth_denied");
  Send is denied break;
  Default://Send return break;
  /** * Obtain the OpenID Accesstoken value for later operation * @param code request code */private void Getaccess_token (final String code) { String Path = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + weixin_app_id + "&secret=" + AP
  P_secret + "&code=" + code + "&grant_type=authorization_code";
  LogUtils.log ("Getaccess_token:" + path); Network requests, according to their own request mode Volleyrequest.get (this, path, "Getaccess_token", false, NULL, new Volleyrequest.callback () {@Overri De 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) {}});  /** * Access to micro-letter personal Information * @param access_token * @param OpenID/private void GETUSERMESG (final String Access_token, Final string OpenID {string path = "https://api.weixin.qq.com/sns/userinfo?access_token=" + Access_token + "&
  Amp;openid= "+ OpenID;
  LogUtils.log ("GETUSERMESG:" + path); Network requests, according to their own request mode Volleyrequest.get (this, path, "Getaccess_token", false, NULL, new Volleyrequest.callback () {@Overri
    De 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 ("User basic 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 up the Registration micro-letter page in Androidmanifest.xml

When authorized to enter the wxentryactivity, the current background may be black or white, in order not to affect the user experience, you can set the current activity to transparent:

Android:theme= "@android: Style/theme.translucent"

<!--registered Micro-letter callback wxentryactivity-->
<activity
 android:name= ". Wxapi. Wxentryactivity "
 android:exported=" true "
 android:label=" @string/app_name "
 android:launchmode=" Singletop "
 android:theme=" @android: Style/theme.translucent "/> 

Specific reference: micro-letter Authorization to enroll user information steps

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.