Android implements the method of using micro-letters to log in to a Third-party app _android

Source: Internet
Author: User
Tags openid

This article illustrates how Android implements a Third-party app that uses micro-letters to log in. Share to everyone for your reference, specific as follows:

Using the micro-letter login app, eliminating the registration process, there are many similar applications now. The integration of this functionality is not complicated, but there are some areas to be aware of.

Before you start, you need to do the following preparation.

1, to the micro-letter open platform to register your app, and apply for the opening of micro-letter login permissions. Reference here:

https://open.weixin.qq.com//

2, download the Android SDK and signature Viewing tool, please refer to:

Https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id= open1419319167&token=&lang=zh_cn/

3, access to the app's signature, and fill in the micro-letter open platform.

The following starts with the topic:

Introduce the micro-letter login-related SDK to the project's Lib folder, one file:

Libammsdk.jar

Invoke micro-letter, request user authorization:

API Registration
API = WXAPIFACTORY.CREATEWXAPI (this, "app_id", true);
Api.registerapp ("app_id");
Sendauth.req Req = new Sendauth.req ();
Authorization to read user information
req.scope = "Snsapi_userinfo";
Custom information
req.state = "Wechat_sdk_demo_test";
Send Request
Api.sendreq (req) to the micro-letter;

Replace the app_id with the one you applied to on the micro-letter open platform, and the code above can be used to tune the micro-mail.

After the authorization is successful, the micro-letter Returns a code, and the following describes how to receive the message from the micro-mail.

Create a new WXAPI directory under your package name and add a Wxentryactivity.java file to the Wxapi directory, and enter code similar to the following:

Package the name of your bag. Wxapi;

The public class Wxentryactivity extends activity implements iwxapieventhandler{@Override the public
  void OnCreate ( Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Registration API
    API = WXAPIFACTORY.CREATEWXAPI (this, "app_id");
    Api.handleintent (Getintent (), this);
  }
  @Override public
  void Onresp (Baseresp resp) {
    if (resp instanceof sendauth.resp) {sendauth.resp Newresp
      = (SENDAUTH.RESP) Resp;
      Get code
      String code = Newresp.code}} for micro-mail
    returns


Add the following code to the Androidmanifest.xml:

Copy Code code as follows:
<activity android:exported= "true" android:launchmode= "Singletop" android:name=. Wxentryactivity "Android:theme=" @android: Style/theme.nodisplay "/>

Fix the error in the wxentryactivity, get code in the Onresp method, and get the token and OpenID via the following interface:

Https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type= Authorization_code

Get the following data:

{"
Access_token": "Access_token", "
expires_in": 7200,
"Refresh_token": "Refresh_token",
"OpenID": "OPENID",
"scope": "Scope",
"Unionid": "O6_BMASDASDSAD6_2SGVT7HMZOPFL"
}

OpenID can be used as the user's unique identification, the OpenID will be saved, you can implement the login status check.

If you need to obtain information about a user, such as a nickname, Avatar, you can use the following interface:

Https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID

The data obtained are:

{
"OpenID": "OpenID", "
nickname": "nickname",
"sex": 1,
"province": "Province",
"City": "City", "
Country": "Country",
"Headimgurl": "http://wx.qlogo.cn/mmopen/ g3monuztnhkdmzicilibx6iafqac56vxlsufpb6n5wksyvy0chqkkiajsgq1dzutogvllrhjberqq4emsv84eavhiaiceqxibjxcfhe/0 ",
"privilege": [
"PRIVILEGE1",
"PRIVILEGE2"
],
"Unionid": "O6_BMASDASDSAD6_2SGVT7HMZOPFL"
}

For more interface use, please refer to:

Https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id= Open1419317853&lang=zh_cn

Possible problems:

1, can not adjust the micro-letter.

The possible cause is a signature error, please check the signature.

2, can not accept the message of micro-letter return

The possible reason is not to add wxentryactivity to the Androidmanifest.xml.

PS: About Androidmanifest.xml file related properties can refer to the site online tools:

Android manifest Features and Permissions description Encyclopedia:
Http://tools.jb51.net/table/AndroidManifest

For more information on Android-related content readers can view the site topics: "Introduction to Android development and advanced tutorials," "Android Database Operating skills summary", "Android Programming Activity Tips Summary", "Android File Operation tips Summary "," Android programming development of SD card operation method Summary "," Android Resource Operation skills Summary "," Android View Overview "and" Android Control usage Summary "

I hope this article will help you with the Android program.

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.