这个网上也有好多,这个是我自己总结出来的,没有多少注释,写的不太规范 ,仅供参考
1.微信登录接口是要money的当你在微信开放平台上开通了登录功能后才可以继续。
创建好项目且审核通过后就会得到appid和 appsecret,这两个参数在程序中会用到
微博登录接口是免费的,首先也要创建好项目
然后在接口管理中填写回调网页
这个好像就是固定的这个网页
创建好项目之后也会得到一个appkey 主要用到的就是这个appkey,
2.先把jar和so文件放到libs文件夹里
3.MainActivity
这个里面是两个按钮的单击事件,分别登录微信微博
package com.example.aichong;
import java.text.SimpleDateFormat;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.example.weibo.AccessTokenKeeper;
import com.sina.weibo.sdk.api.share.IWeiboShareAPI;
import com.sina.weibo.sdk.api.share.WeiboShareSDK;
import com.sina.weibo.sdk.auth.AuthInfo;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.auth.WeiboAuthListener;
import com.sina.weibo.sdk.auth.sso.SsoHandler;
import com.sina.weibo.sdk.exception.WeiboException;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
public class MainActivity extends Activity {
private IWXAPI mWeixinAPI;
String WEIXIN_APP_ID = "你的appid";
String WEIXIN_APP_SECRET = "你的sercert";
public static final String WEIBP_APP_KEY = "你的appkey"; // 应用的APP_KEY
public static final String REDIRECT_URL = "https://api.weibo.com/oauth2/default.html";// 应用的回调页
AuthInfo mAuthInfo;
Oauth2AccessToken mAccessToken;
SsoHandler mSsoHandler;
IWeiboShareAPI mWeiboShareAPI;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initWeibo();
}
private void initWeibo() {
// TODO Auto-generated method stub
mAuthInfo = new AuthInfo(MainActivity.this, WEIBP_APP_KEY,
REDIRECT_URL, "-->");
mWeiboShareAPI = WeiboShareSDK.createWeiboAPI(this, WEIBP_APP_KEY);
mWeiboShareAPI.registerApp(); // 将应用注册到微博客户端
}
// 微博登录
public void toWeibo(View view) {
gotoSina();
}
// 微信登录
public void toWeixin(View view) {
if (mWeixinAPI == null) {
mWeixinAPI = WXAPIFactory.createWXAPI(this, WEIXIN_APP_ID, true);
}
mWeixinAPI.registerApp(WEIXIN_APP_ID);
loginByWeiXin();
}
private void loginByWeiXin() {
// TODO Auto-generated method stub
if (!mWeixinAPI.isWXAppInstalled()) {
return;
}
SendAuth.Req req = new SendAuth.Req();
req.scope = "snsapi_userinfo";
req.state = "wechat_sdk_demo";
mWeixinAPI.sendReq(req);
}
// 微博登录回调
class AuthListener implements WeiboAuthListener {
@Override
public void onComplete(Bundle values) {
// 获取微博用户登录信息
mAccessToken = Oauth2AccessToken.parseAccessToken(values);
String access_token = values.getString("access_token");
String expires_in = values.getString("expires_in");
final String idstr = values.getString("uid");
Log.e("access_token", access_token); // 中解析
Log.e("expires_in", expires_in);
Log.e("idstr", idstr);
mAccessToken = new Oauth2AccessToken(access_token, expires_in);
if (mAccessToken.isSessionValid()) {
AccessTokenKeeper.writeAccessToken(MainActivity.this,
mAccessToken);
String date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
.format(new java.util.Date(mAccessToken
.getExpiresTime()));
AccessTokenKeeper.writeAccessToken(MainActivity.this,
mAccessToken);
// AccessTokenKeeper.readAccessToken(LoginOthersActivity.this);
} else {
// 当您注册的应用程序签名不正确时,就会收到错误Code,请确保签名正确
String code = values.getString("code", "");
Log.e("ErrorCode", code + "------------");
}
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
}
@Override
<span style="white-space:pre"> </span>public void onWeiboException(WeiboException arg0) {
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>private void gotoSina() {
<span style="white-space:pre"> </span>mSsoHandler = new SsoHandler(this, mAuthInfo);
<span style="white-space:pre"> </span>mSsoHandler.authorize(new AuthListener());
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>protected void onActivityResult(int requestCode, int resultCode, Intent data) {
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>super.onActivityResult(requestCode, resultCode, data);
<span style="white-space:pre"> </span>if (mSsoHandler != null) {
<span style="white-space:pre"> </span>mSsoHandler.authorizeCallBack(requestCode, resultCode, data);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
}
建这样一个包,即使在创建项目的那个包名后面加上.wxapi,必须是这样的包名
然后在这个包里面放上WXEntryActivity.java这个api里面应该有,也展示一下吧
</pre><pre code_snippet_id="1610512" snippet_file_name="blog_20160315_5_3537974" name="code" class="java">package com.example.aichong.wxapi;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth;
public class WXEntryActivity extends Activity {
String WEIXIN_APP_ID = "wx2c2489e42549ea70";
String WEIXIN_APP_SECRET = "7b3ba5c20921492f95a8687e6666251c";
final int RETURN_MSG_TYPE_LOGIN = 1;
final int RETURN_MSG_TYPE_SHARE = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
SendAuth.Resp resp = new SendAuth.Resp(intent.getExtras());
if (resp.errCode == BaseResp.ErrCode.ERR_OK) {
if (resp.getType() == RETURN_MSG_TYPE_SHARE) {
}else {
getResultCodes(WEIXIN_APP_ID,WEIXIN_APP_SECRET,resp.code);
// intent = new Intent(this,AboutusActivity.class);
// startActivity(intent);
finish();
}
}
}
private void getResultCodes(String key,String secret,String code){
String weixinUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+key+"&secret="+secret+"&code="+code+"&grant_type=authorization_code";
HttpUtils utils = new HttpUtils();
utils.send(HttpMethod.GET, weixinUrl, new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
// TODO Auto-generated method stub
String result = arg0.result;
try {
JSONObject object = new JSONObject(result);
String openid = object.getString("openid");
// String access_token = object.getString("access_token");
// String refresh_token = object.getString("refresh_token");
// Log.e("refresh_token", "--->"+refresh_token);
Log.e("openid", "--->"+openid);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
以上基本可以实现微信登录了
AccessTokenKeeper.java这个类也是微博api中有的
/*
* Copyright (C) 2010-2013 The SINA WEIBO Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.weibo;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
/**
* 该类定义了微博授权时所需要的参数。
*
* @author SINA
* @since 2013-10-07
*/
public class AccessTokenKeeper {
private static final String PREFERENCES_NAME = "com_weibo_sdk_android";
private static final String KEY_UID = "uid";
private static final String KEY_ACCESS_TOKEN = "access_token";
private static final String KEY_EXPIRES_IN = "expires_in";
private static final String KEY_REFRESH_TOKEN = "refresh_token";
/**
* 保存 Token 对象到 SharedPreferences。
*
* @param context 应用程序上下文环境
* @param token Token 对象
*/
public static void writeAccessToken(Context context, Oauth2AccessToken token) {
if (null == context || null == token) {
return;
}
SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
Editor editor = pref.edit();
editor.putString(KEY_UID, token.getUid());
editor.putString(KEY_ACCESS_TOKEN, token.getToken());
editor.putString(KEY_REFRESH_TOKEN, token.getRefreshToken());
editor.putLong(KEY_EXPIRES_IN, token.getExpiresTime());
editor.commit();
}
/**
* 从 SharedPreferences 读取 Token 信息。
*
* @param context 应用程序上下文环境
*
* @return 返回 Token 对象
*/
public static Oauth2AccessToken readAccessToken(Context context) {
if (null == context) {
return null;
}
Oauth2AccessToken token = new Oauth2AccessToken();
SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
token.setUid(pref.getString(KEY_UID, ""));
token.setToken(pref.getString(KEY_ACCESS_TOKEN, ""));
token.setRefreshToken(pref.getString(KEY_REFRESH_TOKEN, ""));
token.setExpiresTime(pref.getLong(KEY_EXPIRES_IN, 0));
return token;
}
/**
* 清空 SharedPreferences 中 Token信息。
*
* @param context 应用程序上下文环境
*/
public static void clear(Context context) {
if (null == context) {
return;
}
SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
Editor editor = pref.edit();
editor.clear();
editor.commit();
}
}
以上就可以实现微信微博登录了,至于回调回来的参数,api上写的都很详细。
技术不是很好,写的不好,请见谅,谢谢!