1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
| package com.DefaultCompany.ResLoadPrg;
import android.app.Activity; import android.content.Intent; import android.text.TextUtils; import android.util.Log;
import com.tencent.tauth.Tencent; import com.tencent.connect.common.Constants;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class TencentQQ { private static Tencent m_Tencent;
private static Activity m_MainActivity = null; public static final String TAG = TencentQQ.class.getName(); private static final String APP_ID = "1065732495"; private static final BaseUiListener m_LoginCallback = new BaseUiListener(); public static void Init(Activity activity){ Log.i(TAG, "Init: TencentQQ"); m_MainActivity = activity;
m_Tencent = Tencent.createInstance(APP_ID,activity.getApplicationContext()); }
public static void onActivityResult(int requestCode, int resultCode, Intent data){ if(requestCode == Constants.REQUEST_API){ if(resultCode == Constants.REQUEST_LOGIN){ Tencent.handleResultData(data,m_LoginCallback); } Tencent.onActivityResultData(requestCode,resultCode,data,m_LoginCallback); } }
public static void Login(){ Log.i(TAG, "Login: QQLogin"); m_Tencent.login(m_MainActivity,"all",m_LoginCallback); }
public static void Logout(){ Log.i(TAG, "Logoff: QQLogout"); m_Tencent.logout(m_MainActivity); }
public static boolean CheckAuthorValid(){ return m_Tencent.isSessionValid(); }
public static JSONObject RefreshSession(){ JSONObject jsonObject = m_Tencent.loadSession(APP_ID); if(jsonObject == null){ Login(); Log.i(TAG, "RefreshSession: 没有返回票据"); } else { m_Tencent.initSessionCache(jsonObject); Log.i(TAG, "RefreshSession: " + jsonObject); }
return SetSelfData(jsonObject); }
public static void LoginCallBack(final JSONObject jsonObject){ Log.i(TAG, "LoginCallBack: " + jsonObject.toString()); new Thread(new Runnable() { @Override public void run() { InitOpenidAndToken(jsonObject); JSONObject data = SetSelfData(jsonObject); GameHelper.SendPlatformMessageToUnity(GameHelper.PLATFORM_MSG_QQLOGINCALLBACK,0,0,0,data.toString(),"",""); } }).start();
}
public static void InitOpenidAndToken(JSONObject jsonObject){ try{ String token = jsonObject.getString(Constants.PARAM_ACCESS_TOKEN); String expires = jsonObject.getString(Constants.PARAM_EXPIRES_IN); String openid = jsonObject.getString(Constants.OPENID);
if(!TextUtils.isEmpty(token) && !TextUtils.isEmpty(expires) && !TextUtils.isEmpty(openid)){ m_Tencent.setAccessToken(token,expires); m_Tencent.setOpenId(openid); }
} catch (Exception e){ Log.e(TAG, "InitOpenidAndToken: " + e.toString() );
} }
public static JSONObject SetSelfData(JSONObject data){ JSONObject jsonObject = new JSONObject(); try{ String token = data.getString(Constants.PARAM_ACCESS_TOKEN); String expires = data.getString(Constants.PARAM_EXPIRES_IN); String openid = data.getString(Constants.OPENID); String paytoken = data.getString("pay_token"); String pf = data.getString("pf"); String pfkey = data.getString("pfkey"); String expirestime = data.getString(Constants.PARAM_EXPIRES_TIME); String unionid = GetUnionId(token);
jsonObject.put("openid",openid); jsonObject.put("token",token); jsonObject.put("unionid",unionid); jsonObject.put("refreshtoken",""); jsonObject.put("expires",expires); jsonObject.put("paytoken",paytoken); jsonObject.put("pf",pf); jsonObject.put("pfkey",pfkey); jsonObject.put("expirestime",expirestime); } catch (Exception e){ Log.e(TAG, "SetSelfData: 设置统一QQ票据失败"); } return jsonObject; }
public static String GetUnionId(String token){ String unionid = ""; try { URL url = new URL("https://graph.qq.com/oauth2.0/me?access_token=" + token + "&unionid=1"); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000);
int code = connection.getResponseCode(); if(code == HttpsURLConnection.HTTP_OK){ InputStream is = connection.getInputStream(); byte[] data = ReadStream(is); String json = new String(data); json = json.replace("(","").replace(")","").replace("callback",""); JSONObject jsonObject = new JSONObject(json); unionid = jsonObject.getString("unionid"); }
} catch (Exception e){ Log.e(TAG, "GetUnionId: 获取unionid失败" + e.toString()); } return unionid; } public static byte[] ReadStream(InputStream inputStream) throws Exception{ ByteArrayOutputStream bout = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inputStream.read(buffer)) != -1){ bout.write(buffer,0,len); } bout.close(); inputStream.close(); return bout.toByteArray(); } }
|