Android集成第三方微信登录

关山初度尘未洗,策马扬鞭再奋蹄!这篇文章主要讲述Android集成第三方微信登录相关的知识,希望能为你提供帮助。
第一步:在微信开放平台创建安卓应用,需要输入的包名和签名就不用再提吧,不知道的自行百度。 
应用创建完毕后会得到两个值:AppID、AppSecret,用这两个值来请求微信。 
然后去微信开放平台的资源中心 ,选择安卓资源包 ,如图: 

Android集成第三方微信登录

文章图片

注意:输入的包名和签名要和项目中的一致!
第二步:【Android集成第三方微信登录】将安卓资源包中的libammsdk.jar 导入到项目中的libs文件夹下面。 
并在项目中的创建一个包 ,包名必须为 package+wxapi,比如你的包名是com.xxx.Android,那就要创建一个 com.xxx.android.wxapi。然后在这个 包下面 建一个类 ,类名为WXEntryActivity。 
在androidManifest.xml 注册这个类
< activity android:name="com.xxx.android.wxapi.WXEntryActivity" android:configChanges="keyboardHidden|orientation|screenSize" android:exported="true" android:launchMode="singleTop" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" > < /activity>

下面是WXEntryActivity中的代码:
public class WXEntryActivity extends Activity implements IWXAPIEventHandler { private IWXAPI api; private BaseResp resp = null; private String WX_APP_ID = "创建应用后得到的APP_ID"; // 获取第一步的code后,请求以下链接获取access_token private String GetCodeRequest = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID& secret=SECRET& code=CODE& grant_type=authorization_code"; // 获取用户个人信息 private String GetUserInfo = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN& openid=OPENID"; private String WX_APP_SECRET = "创建应用后得到的APP_SECRET"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); api = WXAPIFactory.createWXAPI(this, WX_APP_ID, false); api.handleIntent(getIntent(), this); }// 微信发送请求到第三方应用时,会回调到该方法 @Override public void onReq(BaseReq req) { finish(); }// 第三方应用发送到微信的请求处理后的响应结果,会回调到该方法 @Override public void onResp(BaseResp resp) { String result = ""; if (resp != null) { resp = resp; } switch (resp.errCode) { case BaseResp.ErrCode.ERR_OK: result = "发送成功"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); String code = ((SendAuth.Resp) resp).code; /* * 将你前面得到的AppID、AppSecret、code,拼接成URL 获取access_token等等的信息(微信) */ String get_access_token = getCodeRequest(code); AsyncHttpClient client = new AsyncHttpClient(); client.post(get_access_token, new JsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, JSONObject response) { // TODO Auto-generated method stub super.onSuccess(statusCode, response); try {if (!response.equals("")) { String access_token = response .getString("access_token"); String openid = response.getString("openid"); String get_user_info_url = getUserInfo( access_token, openid); getUserInfo(get_user_info_url); }} catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); finish(); break; case BaseResp.ErrCode.ERR_USER_CANCEL: result = "发送取消"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); finish(); break; case BaseResp.ErrCode.ERR_AUTH_DENIED: result = "发送被拒绝"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); finish(); break; default: result = "发送返回"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); finish(); break; } }/** * 通过拼接的用户信息url获取用户信息 * * @param user_info_url */ private void getUserInfo(String user_info_url) { AsyncHttpClient client = new AsyncHttpClient(); client.get(user_info_url, new JsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, JSONObject response) { // TODO Auto-generated method stub super.onSuccess(statusCode, response); try {System.out.println("获取用户信息:" + response); if (!response.equals("")) { String openid = response.getString("openid"); String nickname = response.getString("nickname"); String headimgurl = response.getString("headimgurl"); }} catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); }@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); api.handleIntent(intent, this); finish(); }/** * 获取access_token的URL(微信) * * @param code *授权时,微信回调给的 * @return URL */ private String getCodeRequest(String code) { String result = null; GetCodeRequest = GetCodeRequest.replace("APPID", urlEnodeUTF8(WX_APP_ID)); GetCodeRequest = GetCodeRequest.replace("SECRET", urlEnodeUTF8(WX_APP_SECRET)); GetCodeRequest = GetCodeRequest.replace("CODE", urlEnodeUTF8(code)); result = GetCodeRequest; return result; }/** * 获取用户个人信息的URL(微信) * * @param access_token *获取access_token时给的 * @param openid *获取access_token时给的 * @return URL */ private String getUserInfo(String access_token, String openid) { String result = null; GetUserInfo = GetUserInfo.replace("ACCESS_TOKEN", urlEnodeUTF8(access_token)); GetUserInfo = GetUserInfo.replace("OPENID", urlEnodeUTF8(openid)); result = GetUserInfo; return result; }private String urlEnodeUTF8(String str) { String result = str; try { result = URLEncoder.encode(str, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } return result; }}

第三步:在Activity中调起访问微信
public class LoginActivity extends Activity implements OnClickListener {private Button login; // 微信登录 private static IWXAPI WXapi; private String WX_APP_ID = "创建应用后得到的APP_ID"; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub // 启动activity时不自动弹出软键盘 getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.login); init(); }private void init() { login = (Button) findViewById(R.id.bt_login); login.setOnClickListener(this); }@Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) {case R.id.login: WXLogin(); break; default: break; }}/** * 登录微信 */ private void WXLogin() { WXapi = WXAPIFactory.createWXAPI(this, WX_APP_ID, true); WXapi.registerApp(WX_APP_ID); SendAuth.Req req = new SendAuth.Req(); req.scope = "snsapi_userinfo"; req.state = "wechat_sdk_demo"; WXapi.sendReq(req); }}

 

    推荐阅读