Detailed graphic and text explanation of the steps for integrating WeChat login on Android

高洛峰
Release: 2017-03-15 17:23:28
Original
2928 people have browsed it

This article will give you a detailed explanation with pictures and textAndroidThe steps to integrate WeChat login. Through the content compiled in the article, you only need a few lines of code to realize the WeChat login function. Friends who are just interested Let’s take a look below.

1. First write in onCreate of Application:

// GeneralAppliction.java
public static IWXAPI sApi;
@Override
public void onCreate() {
 super.onCreate();
 sApi = WXEntryActivity.initWeiXin(this, AppConst.WEIXIN_APP_ID);
}
Copy after login

2. Add where you need to log in:

// MainActivity.java
WXEntryActivity.loginWeixin(MainActivity.this, GeneralAppliction.sApi);
Copy after login

3 ,The specific integration steps are described in detail below.

Integration steps:

1. Register and create an application on the open platform and apply for login permission

2. Download sdk, Copy related files to the project directory

3. Globally initialize the WeChat component

4. Request authorization to log in and obtain the code

5. Obtain through code Authorization password access_token

6. In step 5, determine whether access_token exists and expires

7. If access_token expires and is invalid, use refresh_token to refresh

8. Use access_token to obtain User information

1. Register and create an application on the open platform and apply for login permission

This step does not need to be explained. The only way is to register an account on the WeChat open platform. Then create the mobile app.

Detailed graphic and text explanation of the steps for integrating WeChat login on Android

What needs to be noted is: the application signature part

Detailed graphic and text explanation of the steps for integrating WeChat login on Android

I use the online signature here key'smd5, for this issue that needs attention, you can see: Android signature summary

2. Download the sdk and copy the relevant files to the project directory

Download of development tool kit (SDK): libraries and files required for WeChat sharing, login, collection, payment and other functions

Example Demo

After downloading, copy the libammsdk.jar file to the libs directory of the AS project, and copy the entire wxapi directory under the source file directory in the example Demo to the root package under the src of the project directory:

Detailed graphic and text explanation of the steps for integrating WeChat login on Android

If the wxapi folder is placed in the wrong location, you will not be able to log in, and the WeChat sdk cannot find the login Activity authorization function. Then add in Manifest.xml:

<activity 
 android:name=".wxapi.WXEntryActivity" 
 android:theme="@android:style/Theme.Translucent.NoTitleBar"
 android:configChanges="keyboardHidden|orientation|screenSize"
 android:exported="true"
 android:screenOrientation="portrait" />
Copy after login

3. Globally initialize the WeChat component

Globally initialize the WeChat component, of course, in the onCreate of the Application (of course, the onCreate of the Activity is also possible, in order to use the WeChat api globallyObject to facilitate operation):

@Override
public void onCreate() { 
 super.onCreate();
 // 初始化微信组件
 initWeiXin();
}

public static IWXAPI sApi;
private void initWeiXin() {
 sApi = WXEntryActivity.initWeiXin(this, AppConst.WEIXIN_APP_ID);
}
Copy after login

4. Request authorization to log in and get the code

For the single principle of the same business, I related WeChat All are packaged into the wxapi package and WXEntryActivity:

// 实现IWXAPIEventHandler 接口,以便于微信事件处理的回调
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

 private static final String WEIXIN_ACCESS_TOKEN_KEY = "wx_access_token_key";
 private static final String WEIXIN_OPENID_KEY = "wx_openid_key";
 private static final String WEIXIN_REFRESH_TOKEN_KEY = "wx_refresh_token_key";

 private Gson mGson;
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 // 微信事件回调接口注册
 GeneralAppliction.sApi.handleIntent(getIntent(), this);
 mGson = new Gson();
 }

 /**
 * 微信组件注册初始化
 * @param context 上下文
 * @param weixin_app_id appid
 * @return 微信组件api对象
 *
 /
 public static IWXAPI initWeiXin(Context context, @NonNull String weixin_app_id) {
 if (TextUtils.isEmpty(weixin_app_id)) {
 Toast.makeText(context.getApplicationContext(), "app_id 不能为空", Toast.LENGTH_SHORT).show();
 }
 IWXAPI api = WXAPIFactory.createWXAPI(context, weixin_app_id, true);
 api.registerApp(weixin_app_id);
 return api;
 }

 /** 
 * 登录微信 
 * 
 * @param api 微信服务api 
 */
 public static void loginWeixin(Context context, IWXAPI api) {
 // 判断是否安装了微信客户端 
 if (!api.isWXAppInstalled()) { 
 Toast.makeText(context.getApplicationContext(), "您还未安装微信客户端!", Toast.LENGTH_SHORT).show(); 
 return; 
 }
 // 发送授权登录信息,来获取code
 SendAuth.Req req = new SendAuth.Req(); 
 // 应用的作用域,获取个人信息
 req.scope = "snsapi_userinfo"; 
 /** 
 * 用于保持请求和回调的状态,授权请求后原样带回给第三方
 * 为了防止csrf攻击(跨站请求伪造攻击),后期改为随机数加session来校验 
 */ 
 req.state = "app_wechat";
 api.sendReq(req);
 }

 // 微信发送请求到第三方应用时,会回调到该方法
 @Override
 public void onReq(BaseReq req) { 
 switch (req.getType()) { 
 case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX: 
 break; 
 case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
 break; 
 default: 
 break; 
 }
 }
 // 第三方应用发送到微信的请求处理后的响应结果,会回调到该方法
 @Override
 public void onResp(BaseResp resp) {
 switch (resp.errCode) { 
 // 发送成功 
 case BaseResp.ErrCode.ERR_OK:
 // 获取code
 String code = ((SendAuth.Resp) resp).code;
 // 通过code获取授权口令access_token
 getAccessToken(code);
 break;
 }
 }
}
Copy after login

If you have any questions about what the code is:

The third party needs to use the code to obtain the access_token. The timeout of the code is 10 minutes. A code can only be successfully exchanged for the access_token once and then it will become invalid. The temporary and one-time nature of the code ensures the security of WeChat authorized login. Third parties can further enhance the security of their own authorized logins by using https and state parameters.

In this way, where the client uses only needs to be:

WXEntryActivity.loginWeixin(MainActivity.this, GeneralAppliction.sApi);
Copy after login

5. Obtain through code Authorization password access_token

We obtained the code in the onResp callback method, and then obtained the authorization password access_token through the code:

/** 
* 获取授权口令 
*/
private void getAccessToken(String code) { 
 String url = "https://api.weixin.qq.com/sns/oauth2/access_token?" +
 "appid=" + AppConst.WEIXIN_APP_ID +
 "&secret=" + AppConst.WEIXIN_APP_SECRET +
 "&code=" + code +
 "&grant_type=authorization_code"; 
 // 网络请求获取access_token 
 httpRequest(url, new ApiCallback<String>() { 
 @Override 
 public void onSuccess(String response) { 
 Logger.e(response); 
 // 判断是否获取成功,成功则去获取用户信息,否则提示失败 
 processGetAccessTokenResult(response); 
 } 
 @Override 
 public void onError(int errorCode, final String errorMsg) {
 Logger.e(errorMsg); 
 showMessage("错误信息: " + errorMsg); 
 } 
 @Override 
 public void onFailure(IOException e) {
 Logger.e(e.getMessage()); 
 showMessage("登录失败"); 
 } 
 });
}

/** 
* 处理获取的授权信息结果 
* @param response 授权信息结果 
*/
private void processGetAccessTokenResult(String response) { 
 // 验证获取授权口令返回的信息是否成功
 if (validateSuccess(response)) {
 // 使用Gson解析返回的授权口令信息 
 WXAccessTokenInfo tokenInfo = mGson.fromJson(response, WXAccessTokenInfo.class);
 Logger.e(tokenInfo.toString());
 // 保存信息到手机本地
 saveAccessInfotoLocation(tokenInfo);
 // 获取用户信息 
 getUserInfo(tokenInfo.getAccess_token(), tokenInfo.getOpenid()); 
 } else {
 // 授权口令获取失败,解析返回错误信息 
 WXErrorInfo wxErrorInfo = mGson.fromJson(response, WXErrorInfo.class); 
 Logger.e(wxErrorInfo.toString()); 
 // 提示错误信息
 showMessage("错误信息: " + wxErrorInfo.getErrmsg()); 
 }
}

/** 
* 验证是否成功 
* 
* @param response 返回消息 
* @return 是否成功 
*/
private boolean validateSuccess(String response) { 
 String errFlag = "errmsg"; 
 return (errFlag.contains(response) && !"ok".equals(response)) 
 || (!"errcode".contains(response) && !errFlag.contains(response));
}
Copy after login

6. Determine whether access_token exists and expires in step 5

在回调的onResp方法中获取code后,处理access_token是否登录过或者过期的问题:

// 从手机本地获取存储的授权口令信息,判断是否存在access_token,不存在请求获取,存在就判断是否过期
String accessToken = (String) ShareUtils.getValue(this, WEIXIN_ACCESS_TOKEN_KEY, "none");
String openid = (String) ShareUtils.getValue(this, WEIXIN_OPENID_KEY, "");
if (!"none".equals(accessToken)) {
 // 有access_token,判断是否过期有效
 isExpireAccessToken(accessToken, openid);
} else {
 // 没有access_token
 getAccessToken(code);
}
Copy after login

判断授权口令是否有效:

/** 
* 判断accesstoken是过期 
* @param accessToken token 
* @param openid 授权用户唯一标识 
*/
private void isExpireAccessToken(final String accessToken, final String openid) {
 String url = "https://api.weixin.qq.com/sns/auth?" + 
 "access_token=" + accessToken + 
 "&openid=" + openid;
 httpRequest(url, new ApiCallback<String>() {
 @Override
 public void onSuccess(String response) {
 Logger.e(response);
 if (validateSuccess(response)) {
 // accessToken没有过期,获取用户信息
 getUserInfo(accessToken, openid);
 } else {
 // 过期了,使用refresh_token来刷新accesstoken
 refreshAccessToken();
 }
 }
 @Override
 public void onError(int errorCode, final String errorMsg) {
 Logger.e(errorMsg);
 showMessage("错误信息: " + errorMsg);
 }
 @Override
 public void onFailure(IOException e) {
 Logger.e(e.getMessage());
 showMessage("登录失败");
 }
 });
}
Copy after login

7. 如果access_token过期无效,就用refresh_token来刷新

/**
 * 刷新获取新的access_token
 *
/
private void refreshAccessToken() {
 // 从本地获取以存储的refresh_token
 final String refreshToken = (String) ShareUtils.getValue(this, WEIXIN_REFRESH_TOKEN_KEY, "");
 if (TextUtils.isEmpty(refreshToken)) {
 return;
 }
 // 拼装刷新access_token的url请求地址
 String url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?" +
 "appid=" + AppConst.WEIXIN_APP_ID +
 "&grant_type=refresh_token" +
 "&refresh_token=" + refreshToken;
 // 请求执行
 httpRequest(url, new ApiCallback<String>() {
 @Override
 public void onSuccess(String response) {
 Logger.e("refreshAccessToken: " + response);
 // 判断是否获取成功,成功则去获取用户信息,否则提示失败
 processGetAccessTokenResult(response);
 }
 @Override
 public void onError(int errorCode, final String errorMsg) {
 Logger.e(errorMsg);
 showMessage("错误信息: " + errorMsg);
 // 重新请求授权
 loginWeixin(WXEntryActivity.this.getApplicationContext(), GeneralAppliction.sApi);
 }
 @Override
 public void onFailure(IOException e) {
 Logger.e(e.getMessage());
 showMessage("登录失败");
 // 重新请求授权
 loginWeixin(WXEntryActivity.this.getApplicationContext(), GeneralAppliction.sApi);
 }
 });
}
Copy after login

8. 使用access_token获取用户信息

/**
 * 获取用户信息
 *
/
private void getUserInfo(String access_token, String openid) {
 String url = "https://api.weixin.qq.com/sns/userinfo?" +
 "access_token=" + access_token +
 "&openid=" + openid;
 httpRequest(url, new ApiCallback<String>() {
 @Override
 public void onSuccess(String response) {
 // 解析获取的用户信息
 WXUserInfo userInfo = mGson.fromJson(response, WXUserInfo.class);
 Logger.e("用户信息获取结果:" + userInfo.toString()); }
 @Override
 public void onError(int errorCode, String errorMsg) {
 showMessage("错误信息: " + errorMsg);
 }
 @Override
 public void onFailure(IOException e) {
 showMessage("获取用户信息失败");
 }
 });
}
Copy after login

通信部分

private OkHttpClient mHttpClient = new OkHttpClient.Builder().build();
private Handler mCallbackHandler = new Handler(Looper.getMainLooper());
/**
 * 通过Okhttp与微信通信
 * * @param url 请求地址
 * @throws Exception
 */
public void httpRequest(String url, final ApiCallback<String> callback) {
 Logger.e("url: %s", url);
 final Request request = new Request.Builder()
 .url(url)
 .get()
 .build();
 mHttpClient.newCall(request).enqueue(new Callback() {
 @Override
 public void onFailure(Call call, final IOException e) {
 if (callback != null) {
 mCallbackHandler.post(new Runnable() {
  @Override
  public void run() {
  // 请求失败,主线程回调
  callback.onFailure(e);
  }
 });
 }
 }
 @Override
 public void onResponse(Call call, final Response response) throws IOException {
 if (callback != null) {
 if (!response.isSuccessful()) {
  mCallbackHandler.post(new Runnable() {
  @Override
  public void run() {
  // 请求出错,主线程回调
  callback.onError(response.code(), response.message());
  }
  });
 } else {
  mCallbackHandler.post(new Runnable() {
  @Override
  public void run() {
  try {
  // 请求成功,主线程返回请求结果
  callback.onSuccess(response.body().string());
  } catch (final IOException e) {
  // 异常出错,主线程回调
  mCallbackHandler.post(new Runnable() {
   @Override
   public void run() {
   callback.onFailure(e);
   }
  });
  }
  }
  });
 }
 }
 }
 });
}

// Api通信回调接口
public interface ApiCallback<T> {
 /**
 * 请求成功
 *
 * @param response 返回结果
 */
 void onSuccess(T response);
 /**
 * 请求出错
 *
 * @param errorCode 错误码
 * @param errorMsg 错误信息
 */
 void onError(int errorCode, String errorMsg);
 /**
 * 请求失败
 */
 void onFailure(IOException e);
}
Copy after login

总结

集成的详细描述就这样,至于获取的用户信息,小伙伴们应该知道后续自己业务的需求,该怎么处理了。以上就是本文的全部内容了,希望能对大家的学习或者工作带来一定的帮助,如果有疑问大家可以留言交流。

The above is the detailed content of Detailed graphic and text explanation of the steps for integrating WeChat login on Android. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template