Home > Article > WeChat Applet > Detailed graphic and text explanation of Android development WeChat authorized login and WeChat sharing analysis
This article mainly introduces the detailed explanation with pictures and textsAndroid Development of WeChat authorized login and WeChat sharing analysis. It has certain reference value. If you need it, you can learn more.
Preface
In the wave of mobile Internet, Internet-connected APPs have killed single-machine devices on the beach. Many companies hope that their applications can have an account system, but many users do not necessarily buy it. : Why should I register an account for your application? Weibo, WeChat, and QQ have almost become must-install applications on everyone’s mobile phone, so WeChat, Weibo, and QQ said: Come on, come on, you can use my account to log in to your home applications, as long as you follow OAuth2.0 Just the protocol standard. Therefore, third-party social account login has become the choice of many emerging applications. Since the online documents of Tencent’s official WeChat open platform are somewhat different from the latest SDK, and the structural order of login-related documents is somewhat disordered, I will record some of my experiences here today. , to organize the official online documents of the WeChat open platform. At the same time, WeChat sharing can expand the influence of your own APP, so the WeChat sharing function is also a function that many developers need. I have compiled it here, hoping to be helpful to future friends in the same field.
WeChat login
The following content is excerpted from Tencent Open Platform: https://open.weixin.qq.com/cgi -bin/showdocument?action=dir_list&t=resource/res_list&verif y=1&id=open1419317851&token=6bfe3acd2969037c7217667f24f8eeaf714e5113&lang=zh_CN
Authorization process description
WeChat OAuth2.0 authorized login allows WeChat users to use WeChat identitySecurity Log in to third-party applications or websites. After the WeChat user authorizes to log in to the third-party application that has accessed WeChat OAuth2.0, the third party can obtain the user's Interface calling credentials (access_token), through access_token, the authorization relationship interface of the WeChat open platform can be called, so as to obtain the basic open information of WeChat users and help users realize basic open functions.
WeChat OAuth2.0 authorized login currently supports authorization_code mode, which is suitable for application authorization with server side. The overall process of this model is:
1. A third party initiates a WeChat authorized login request. After the WeChat user allows authorization of the third-party application, WeChat will launch the application or redirect to the third-party website, and bring Authorize the temporary ticket code parameter;
2. Add AppID and AppSecret through the code parameter, and exchange for access_token through API;
3. Pass access_token makes an interface call to obtain the user's basic data resources or help the user implement basic operations.
Get access_token sequence diagram:
Note: If the developer needs to call the login interface, developer authentication needs to be performed and submitted 300Dayang, the official online document says that there is no need to pay. In fact, that is the past tense, but the online document has not been updated.
The following will explain the WeChat authorized login process in sequence. All network requests are GET requests.
1. Get temporary ticket code
2. Get access_token & openid
3. Check whether access_token is valid
4. Refresh or renew access_token
5. Get WeChat user details
Get temporary ticket code
The first three arrows pointing to the right
{ // 发出授权申请 Final SendAuth.Req req = new SendAuth.Req(); req.scope = "snsapi_userinfo"; req.state = "wechat_sdk_微信登录,分享demo_test"; api.sendReq(req); }
The process of the two arrows pointing to the left is reflected in the code:
public void onResp(BaseResp resp) ;// 这个回调接口位于IWXAPIEventHandler中
The returned data is resp. When used to request login authorization, it is an instance of SendAuth.Resp. The data carried is:
ErrorCode: ERR_OK = 0 (user agrees); ERR_AUTH_DENIED = -4 (user refuses authorization); ERR_USER_CANCEL = -2 (user cancels)
code: The code that the user exchanges for access_token, Only valid when ErrCode is 0
state: A flag used by a third-party program to identify the uniqueness of its request when sending. It is passed in when the third-party program calls sendReq and is sent by the WeChat terminal. Return, state string length cannot exceed 1K
lang:微信客户端当前语言
country:微信客户端当前国家
以上数据均以static String形式存在SendAuth.Resp的resp对象中。
注意:当使用微信提供最新的SDK/library时,上面有些数据是不存在,微信开放平台的文档和API及SDK没有同步更新。读者可使用最下方微信登录,分享demo中的笔者使用的jar包构建工程。
获取access_token & openid
最后一条向右的箭头表示:使用得到的code,获取access_token,openid,接口为:
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
注意:微信的接口链接是使用SSL的安全链接,普通的HttpClient访问会导致应用崩溃或报错,详细方法请下载最下方的微信登录,分享demo代码
参数说明
appid: 应用唯一标识,在微信开放平台提交应用审核通过后获得
secret:应用密钥AppSecret,在微信开放平台提交应用审核通过后获得
code :填写第一步获取的code参数
grant_type:固定值,填authorization_code
最下方向左的箭头表示使用code访问完链接返回的数据,json携带的数据有:
access_token:接口调用凭证
expires_in:access_token的有效期,一般为7200(秒),也即是两小时
refresh_token:用户刷新access_token
openid:授权用户唯一标识
scope:用户授权的作用域,使用逗号(,)分隔
检查access_token是否有效
由于access_token有效期为两小时,所以进行下一步操作前最好进行一次检查,接口为:
https://api.weixin.qq.com/sns/auth?access_token=ACCESS_TOKEN&openid=OPENID
传入的参数为accesss_token和openid。
access_token有效时返回的json是:
{ "errcode":0,"errmsg":"ok" }
失效时的返回数据为:
{ "errcode":40003,"errmsg":"invalid openid" }
如果access_token有效,则跳过下一步,失效时需要刷新或续期access_token。
刷新或续期access_token
接口说明
access_token是调用授权关系接口的调用凭证,由于access_token有效期(目前为2个小时)较短,当access_token超时后,可以使用refresh_token进行刷新,access_token刷新结果有两种:
1.若access_token已超时,那么进行refresh_token会获取一个新的access_token,新的超时时间;
2.若access_token未超时,那么进行refresh_token不会改变access_token,但超时时间会刷新,相当于续期access_token。
refresh_token拥有较长的有效期(30天),当refresh_token失效的后,需要用户重新授权。
刷新accessToken接口为:
https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
参数说明:
appid:应用唯一标识
grant_type:固定值,填refresh_token
refresh_token:填写前面获取到的refresh_token的值
返回的json数据有:
access_token:接口调用凭证
expires_in:access_token接口调用凭证超时时间,单位(秒)
refresh_token:用户刷新access_token
openid:授权用户唯一标识
scope:用户授权的作用域,使用逗号(,)分隔
获取微信用户详细信息
获取access_token,openid后,就可以用来获取更多用户信息,比如微信昵称,头像,性别等。接口为:
https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID
可获取的json携带的数据有:
openid:普通用户的标识,对当前开发者帐号唯一
nickname:普通用户昵称
sex:普通用户性别,1为男性,2为女性
province:普通用户个人资料填写的省份
city:普通用户个人资料填写的城市
country:国家,如中国为CN
headimgurl:用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空
privilege:用户特权信息,json数组,如微信沃卡用户为(chinaunicom)
unionid:用户统一标识。针对一个微信开放平台帐号下的应用,同一用户的unionid是唯一的。
微信官方建议:
开发者最好保存unionID信息,以便以后在不同应用之间进行用户信息互通。
微信登录的流程结束了, 至于开发者需要将那些用户信息上传到自家的app服务器就取决于开发者了。
微信分享
1、微信分享分为微信好友分享,朋友圈分享,当然,还有收藏也是共用分享的接口,无需授权登录即可调用分享接口。
2、由于好友分享,朋友圈分享和收藏只是一个参数的区别,所以下面只讲好友分享,具体的可以下载最下方的微信登录,分享demo源码进行查看。
3、微信可以分享的内容包括,纯文字,图片,链接,音乐,视频,app,emoji表情等。
微信分享流程
1、在你的工程里面新建一个wxapi包,并且新建一个WXEntryActivity,继承Activity,或其他Activity(这两步是必须的,微信开发文档中有提到),详见:
https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=1417751808&token=&lang=zh_CN
2、并在manifest文件里面加上exported属性,设置为true。
3、实现一个IWXAPIEventHandler接口。
微信发送的请求将回调到onReq方法,发送到微信请求的响应结果将回调到onResp方法
在WXEntryActivity中将接收到的intent及实现了IWXAPIEventHandler接口的对象传递给IWXAPI接口的handleIntent方法,示例如下:
api.handleIntent(getInent(),this);
当微信发送请求到你的应用,将通过IWXAPIEventHandler接口的onReq方法进行回调,类似的,应用请求微信的响应结果将通过onResp回调。
注意
如果需要混淆代码,为了保证sdk的正常使用,需要在proguard.cfg加上下面两行配置:
-keep class com.tencent.mm.sdk.** { *; }
微信分享详细代码流程是:
IWXAPI api = WXAPIFactory.createWXAPI(this, APP_ID, false);// 传入申请到的appid,得到一个IWXAPI的实例 api.registerApp(APP_ID);// 将app注册到微信列表,我不知道这是什么意思,有知道的请告诉我,谢谢! // 开始分享纯文本到给好友 WXTextObject textObj = new WXTextObject(); textObj.text = text; // 用WXTextObject对象初始化一个WXMediaMessage对象 WXMediaMessage msg = new WXMediaMessage(); msg.mediaObject = textObj; // 发送文本类型的消息时,title字段不起作用 // msg.title = "Will be ignored"; msg.title = "分享文字标题"; msg.description = text; // 构造一个Req SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("text"); // transaction字段用于唯一标识一个请求 req.message = msg; req.scene = SendMessageToWX.Req.WXSceneTimeline;// 表示发送场景为朋友圈,这个代表分享到朋友圈 // req.scene = SendMessageToWX.Req.WXSceneSession;//表示发送场景为好友对话,这个代表分享给好友 // req.scene = SendMessageToWX.Req.WXSceneFavorite;// 表示发送场景为收藏,这个代表添加到微信收藏 // 调用api接口发送数据到微信 api.sendReq(req);
上面大致的表现了一个分享纯文本给好友的场景,如果需要分享到朋友圈,只需要更改req.scene字段值。
1、其中IWXAPI.registerAPP(APP_ID)是官方demo中的一行代码,表示的是将app注册到微信列表,我并不知道有什么用,所谓的微信列表出现在哪儿?该行代码删除后,仍然可以获取登录授权,实现分享等功能。有知道的请告诉我,谢谢!
2、目前笔者遇到无法分享在线图片WXImageObject的问题,分享在线图片时出现分享界面右上角“发送”按钮灰色,无法点击的情况,希望分享成功的朋友告诉我,谢谢!问题如下图
要分享链接,图片,音乐,视频等需要将WXTextObject 对象改成对应的obj对象。详细请下载文章下方的微信登录,分享demo。
后记
由于微信官方demo中并未提供微信登录的代码示例,分享的代码很齐全,可是分享在线图片的代码在我这里却又问题,所以笔者将自己的一些经验和遇到的坑总结在这里,包括了微信第三方登录,微信分享的内容,希望对大家有所帮助。也希望笔者在文中提到的问题有热心人能够解答
//1、 注册到微信列表有什么用,微信列表在哪儿可以看到 IWXAPI.registerApp(APP_ID); //2、 我为什么无法使用以下代码分享在线图片 WXImageObject imgObj = new WXImageObject(); imgObj.imageUrl = imgUrl;// 在线图片链接 WXMediaMessage msg = new WXMediaMessage(); msg.mediaObject = imgObj; Bitmap bmp = BitmapFactory.decodeStream(new URL(url).openStream()); Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true); bmp.recycle(); msg.thumbData = Util.bmpToByteArray(thumbBmp, true); SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("img"); req.message = msg; req.scene = isTimelineCb.isChecked() ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession; api.sendReq(req);
最近有人向我反映生成的apk无法正常运行。在此进行解释:
The reason why the apk generated by the demo source code cannot run normally is that when adding an application to the WeChat open platform, the package name, application signature, and app_id are bound. The apk you signed does not work, and the demo code is for reference and communication only.
The above is the detailed content of Detailed graphic and text explanation of Android development WeChat authorized login and WeChat sharing analysis. For more information, please follow other related articles on the PHP Chinese website!