Unity3D接入微信登录SDK安卓版
来源:互联网 发布:万方经济统计数据库 编辑:程序博客网 时间:2024/04/29 22:25
将compile ‘com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+’加入到APP目录下的build.gradle中
以下内容在android studio中的内容:
package com.yjkj.zhuyun.wxapi;import android.content.Intent;import android.os.Bundle;import com.tencent.mm.opensdk.modelbase.BaseReq;import com.tencent.mm.opensdk.modelbase.BaseResp;import com.tencent.mm.opensdk.modelmsg.SendAuth;import com.tencent.mm.opensdk.openapi.IWXAPI;import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;import com.tencent.mm.opensdk.openapi.WXAPIFactory;import com.unity3d.player.UnityPlayer;import com.unity3d.player.UnityPlayerActivity;import org.json.JSONObject;import java.io.InputStreamReader;import java.net.URL;import javax.net.ssl.HttpsURLConnection;public class WXEntryActivity extends UnityPlayerActivity implements IWXAPIEventHandler{ public IWXAPI api; //微信APP_ID UnityPlayerActivity public static final String AppId = "你的APPID"; public static final String AppSecret = "你的APPSecret"; public static final String url1 = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="; public String furl, a_t_r,openId; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); api = WXAPIFactory.createWXAPI(this, AppId, true); api.registerApp(AppId); api.handleIntent(getIntent(), this); } //U3D的网络刷新方法 protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); api.handleIntent(intent, this); } public void SdkLogin() //微信登录请求 , 这步执行成功后会弹出微信 { SendAuth.Req req = new SendAuth.Req(); req.scope = "snsapi_userinfo"; req.state = "wechat_sdk_demo_test"; api.sendReq(req); } @Override public void onReq(BaseReq baseReq) { } //发送到微信请求的响应结果 @Override public void onResp(BaseResp baseResp) { System.out.println("微信响应" + baseResp.errCode); switch (baseResp.errCode) { case BaseResp.ErrCode.ERR_OK: System.out.println("用户同意"); SendAuth.Resp newResp = (SendAuth.Resp) baseResp; String code = newResp.code; furl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + AppId + "&secret=" + AppSecret + "&code=" + code + "&grant_type=authorization_code"; HTTPrequest http = new HTTPrequest(); http.start(); break; case BaseResp.ErrCode.ERR_AUTH_DENIED: System.out.println("用户拒绝授权" + baseResp.openId); break; case BaseResp.ErrCode.ERR_USER_CANCEL: System.out.println("用户取消"); break; } } public class HTTPrequest extends Thread { public void run() { String urlget=furl; a_t_r=""; try { URL url = new URL(urlget); HttpsURLConnection httpURLConnection = (HttpsURLConnection) url.openConnection(); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); httpURLConnection.setConnectTimeout(10000); httpURLConnection.setRequestMethod("GET"); httpURLConnection.connect(); InputStreamReader bis = new InputStreamReader(httpURLConnection.getInputStream(),"utf-8"); int c = 0; while((c = bis.read()) != -1){ a_t_r=a_t_r+(char)c; } System.out.println("HTTP通信成功!"+a_t_r); }catch (Exception e) { System.out.println(urlget + " HTTP通信失败"); a_t_r = "http error"; } if (a_t_r!="http error"){ if(openId == null) //json暂时都是在unity中解开.... 这里是调用unity解http回应的json UnityPlayer.UnitySendMessage("UI_Root","json_AcToken",a_t_r); else UnityPlayer.UnitySendMessage("UI_Root","json_UserInfo",a_t_r); } } }//unity 解 出access_token , 和 openID 后找微信服务器拉用户信息 public void GetUserInfoReq(String ac_token, String openID) { // openId = openID; this.furl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + ac_token + "&openid=" + openID; HTTPrequest tt = new HTTPrequest(); tt.start(); }}以下内容在unity3D中
/// <summary>/// 带返回值的Call /// </summary>/// <typeparam name="T">返回值</typeparam>/// <param name="apiName">方法名</param>/// <param name="args">参数</param>/// <returns></returns>public T _CallSdkApiReturn<T>(string apiName, params object[] args){ AndroidJavaObject jo = _GetCurrentAndroidJavaObject(); return jo.Call<T>(apiName, args);}public void _CallSdkApi(string apiName, params object[] args){ AndroidJavaObject jo = _GetCurrentAndroidJavaObject(); jo.Call(apiName, args);}/// <summary>/// 通过code获取access_token和openid/// </summary>/// <param name="str"></param>public void json_AcToken(string str){ Dictionary<string, object> JsonGet = MiniJSON.Json.Deserialize(str) as Dictionary<string, object>; string openid1 = JsonGet["openid"].ToString(); string access_token = JsonGet["access_token"].ToString(); string[] str2 = new string[] { access_token, openid1 }; _CallSdkApi("GetUserInfoReq", str2);//调用jar包中的方法 Debug.Log("openid为:" + openid1 + "access_token为:" + access_token);}
/// <summary> /// 通过access_token和openid获取用户个人信息 /// </summary> /// <param name="str"></param> public void json_UserInfo(string str) { Dictionary<string, object> JsonGet = MiniJSON.Json.Deserialize(str) as Dictionary<string, object>; string openid = JsonGet["openid"].ToString(); string nickname = JsonGet["nickname"].ToString(); string sex = JsonGet["sex"].ToString(); string province = JsonGet["province"].ToString(); string city = JsonGet["city"].ToString(); string country = JsonGet["country"].ToString(); string headimgurl = JsonGet["headimgurl"].ToString(); //头像是一个链接还得下载下来..... string privilege = JsonGet["privilege"].ToString(); string unionid = JsonGet["unionid"].ToString(); Debug.Log("用户信息: " + "\nopenID:" + openid + "\nnickname:" + nickname + "\nsex" + sex + "\nprovince" + province + "\ncity" + city + "\ncountry" + country + "\nheadimgurl" + headimgurl + "\nunionID" + unionid); }
阅读全文
0 0
- Unity3D接入微信登录SDK安卓版
- unity3D接入微信登录SDKIOS版
- Android微信SDK登录功能接入
- ios接入微信sdk
- ios 接入微信sdk
- 应用接入微信登录
- Android微信登录接入
- cocos2dx3.15接入微信SDK实现登录和分享android studio2.3.3
- unity3d android sdk接入
- Unity IOS微信SDK接入
- java接入微信js-sdk
- 第三方SDK接入--微信
- Android微信SDK接入总结
- iOS -- 接入微信SDK包报错
- 接入第三方SDK-微信支付
- Unity 接入微信sdk安卓篇
- Cocos项目中接入微信SDK
- Android微信SDK分享功能接入
- Ubuntu搭建wordpress开发环境记录
- caffe-multilabel classification
- Lesson 2:作业2:打印当前时间。学习使用Date类和Calendar类。
- 使用Python统计字符串中单词数量
- Fragment中getActivity()和getContext()为空的问题
- Unity3D接入微信登录SDK安卓版
- bash之命令替换(command substitution)
- STM32关于RTC的一些有意思的发现
- I2C总线时序解析
- Java学习笔记—使用dbcp2数据库连接池方式连接测试数据库
- Windows系统让apache使用其他名称的配置文件修改配置文件名称指定自己的配置文件
- 二叉树的下一个结点
- c语言中 int *p = NULL 和 *p = NULL 有什么区别
- bash之算术扩展(arithmetic expansion)