您的位置:首页 > 其它

电商 手机web端使用第三方登录(QQ,sina微博)demo

2017-10-29 10:46 633 查看
QQ 使用的是 腾讯·开放评台

//使用QQ登录的demo

public static void main(String[] args) {
String grant_type = "authorization_code";
String client_id = "******";
String client_secret = "***********";
String code = "**************";//code 由前台访问第三方获取
String redirect_uri = "*************8";
//请求 通过Authorization Code获取Access Token
String url1 = "https://graph.qq.com/oauth2.0/token?grant_type=" + grant_type + "&client_id=" + client_id + "&client_secret=" + client_secret + "&code=" + code + "&redirect_uri=" + redirect_uri;
String backData1 = HttpUtils.sendGetRequest(url1, "utf-8", 10000);
System.out.println(backData1);
String[] split = backData1.split("&");
String at="";
String ei="";
String rt="";
for(String s :split){
int i = s.indexOf("access_token=");
if(s.indexOf("access_token=")!=-1){
at = s.replaceAll("access_token=","");
}
if(s.indexOf("expires_in=")!=-1){
ei = s.replaceAll("expires_in=","");
}
if(s.indexOf("refresh_token=")!=-1){
rt = s.replaceAll("refresh_token=","");
}
}
//        String at =split[0].indexOf("access_token=")==0?split[0].replaceAll("access_token=",""):"";
//        String ei =split[1].indexOf("expires_in=")==0?split[0].replaceAll("expires_in=",""):"";
//        String rt =split[2].indexOf("refresh_token=")==0?split[0].replaceAll("refresh_token=",""):"";
//        JSONObject jsonObject = getJsonObject(backData1);
//        String at = (String) jsonObject.get("access_token");
//        String ei = (String) jsonObject.get("expires_in");
//        String rt = (String) jsonObject.get("refresh_token");
System.out.println(at);
System.out.println(ei);
System.out.println(rt);
System.out.println("===============1============");
//获取用户OpenID
String url2 = "https://graph.qq.com/oauth2.0/me?access_token=" + at;
String backData2 = HttpUtils.sendGetRequest(url2, "utf-8", 10000);
String getSignInfo = backData2.substring(backData2.indexOf("(") + 1, backData2.indexOf(")"));
String ci = (String) JSONObject.fromObject(getSignInfo).get("client_id");
String o = (String) JSONObject.fromObject(getSignInfo).get("openid");

System.out.println("===============2============");
System.out.println(ci);
System.out.println("ACESSTOKEN"+at);
System.out.println("OPENID= "+o);
//调用OpenAPI接口,来获取或修改用户个人信息
String url3 = "https://graph.qq.com/user/get_user_info?access_token=" + at + "&oauth_consumer_key=" + ci + "&openid=" + o;
String backData3 = HttpUtils.sendGetRequest(url3, "utf-8", 10000);
System.out.println(backData3);
Integer ret = (Integer) JSONObject.fromObject(backData3).get("ret");
System.out.println(ret);
String msg = (String) JSONObject.fromObject(backData3).get("msg");
System.out.println(msg);
Integer is_lost = (Integer) JSONObject.fromObject(backData3).get("is_lost");
System.out.println(is_lost);
String nickname = (String) JSONObject.fromObject(backData3).get("nickname");
System.out.println(nickname);
String gender = (String) JSONObject.fromObject(backData3).get("gender");
System.out.println(gender);
String province = (String) JSONObject.fromObject(backData3).get("province");
System.out.println(province);
String city = (String) JSONObject.fromObject(backData3).get("city");
System.out.println(city);
String year = (String) JSONObject.fromObject(backData3).get("year");
System.out.println(year);
String figureurl = (String) JSONObject.fromObject(backData3).get("figureurl");
System.out.println(figureurl);
String figureurl_1 = (String) JSONObject.fromObject(backData3).get("figureurl_1");
System.out.println(figureurl_1);
String figureurl_2 = (String) JSONObject.fromObject(backData3).get("figureurl_2");
System.out.println(figureurl_2);
String figureurl_qq_1 = (String) JSONObject.fromObject(backData3).get("figureurl_qq_1");
System.out.println(figureurl_qq_1);
String figureurl_qq_2 = (String) JSONObject.fromObject(backData3).get("figureurl_qq_2");
System.out.println(figureurl_qq_2);
String is_yellow_vip = (String) JSONObject.fromObject(backData3).get("is_yellow_vip");
System.out.println(is_yellow_vip);
String vip = (String) JSONObject.fromObject(backData3).get("vip");
System.out.println(vip);
String yellow_vip_level = (String) JSONObject.fromObject(backData3).get("yellow_vip_level");
System.out.println(yellow_vip_level);
String level = (String) JSONObject.fromObject(backData3).get("level");
System.out.println(level);
String is_yellow_year_vip = (String) JSONObject.fromObject(backData3).get("is_yellow_year_vip");
System.out.println(is_yellow_year_vip);

}

新浪,是用的 微博·开放平台

//使用新浪微博登录的demo

public static void main(String[] args) throws IOException {
String grant_type = "authorization_code";
String client_id = "***********";
String client_secret = "**********";
String code = "***************8";//code 由前台访问第三方获取

String redirect_uri = "***************88";
//1
String url1 = "https://api.weibo.com/oauth2/access_token";

Map<String, String> params1 = new HashMap<String, String>();
params1.put("client_id", client_id);
params1.put("client_secret", client_secret);
params1.put("grant_type", grant_type);
params1.put("code", code);
params1.put("redirect_uri", redirect_uri);
String post1 = HttpUtils.post(url1, params1);
System.out.println(post1);
String access_token = (String) JSONObject.fromObject(post1).get("access_token");
String remind_in = (String) JSONObject.fromObject(post1).get("remind_in");
Integer expires_in = (Integer) JSONObject.fromObject(post1).get("expires_in");
String uid = (String) JSONObject.fromObject(post1).get("uid");
String isRealName = (String) JSONObject.fromObject(post1).get("isRealName");
System.out.println(access_token);
System.out.println(remind_in);
System.out.println(expires_in);
System.out.println(uid);
System.out.println(isRealName);
//2
String url2 = "https://api.weibo.com/oauth2/get_token_info";
Map<String, String> params2 = new HashMap<String, String>();
params2.put("access_token", access_token);
String post2 = HttpUtils.post(url2, params2);
System.out.println(post2);
//        String uid = (String) JSONObject.fromObject(post1).get("uid");
String appkey = (String) JSONObject.fromObject(post1).get("appkey");
String scope = (String) JSONObject.fromObject(post1).get("scope");
String create_at = (String) JSONObject.fromObject(post1).get("create_at");
String expire_in = (String) JSONObject.fromObject(post1).get("expire_in");

//3 获取用户信息 这里必须传参数uid  开放平台的文档中,参数说明这个参数可以不传
//但是如果不穿这个参数,获取不到用户信息

b88a
String url3 = "https://api.weibo.com/2/users/show.json?access_token=" + access_token+"&uid="+uid;

String backData3 = HttpUtils.sendGetRequest(url3, "utf-8", 10000);

System.out.println(backData3);
//        Integer ret = (Integer) JSONObject.fromObject(backData3).get("ret");
//        System.out.println(ret);
//        String msg = (String) JSONObject.fromObject(backData3).get("msg");
//        System.out.println(msg);
//        Integer is_lost = (Integer) JSONObject.fromObject(backData3).get("is_lost");
//        System.out.println(is_lost);
//        String nickname = (String) JSONObject.fromObject(backData3).get("nickname");
//        System.out.println(nickname);
//        String gender = (String) JSONObject.fromObject(backData3).get("gender");
//        System.out.println(gender);
//        String province = (String) JSONObject.fromObject(backData3).get("province");
//        System.out.println(province);
//        String city = (String) JSONObject.fromObject(backData3).get("city");
//        System.out.println(city);
//        String year = (String) JSONObject.fromObject(backData3).get("year");
//        System.out.println(year);
//        String figureurl = (String) JSONObject.fromObject(backData3).get("figureurl");
//        System.out.println(figureurl);
//        String figureurl_1 = (String) JSONObject.fromObject(backData3).get("figureurl_1");
//        System.out.println(figureurl_1);
//        String figureurl_2 = (String) JSONObject.fromObject(backData3).get("figureurl_2");
//        System.out.println(figureurl_2);
//        String figureurl_qq_1 = (String) JSONObject.fromObject(backData3).get("figureurl_qq_1");
//        System.out.println(figureurl_qq_1);
//        String figureurl_qq_2 = (String) JSONObject.fromObject(backData3).get("figureurl_qq_2");
//        System.out.println(figureurl_qq_2);
//        String is_yellow_vip = (String) JSONObject.fromObject(backData3).get("is_yellow_vip");
//        System.out.println(is_yellow_vip);
//        String vip = (String) JSONObject.fromObject(backData3).get("vip");
//        System.out.println(vip);
//        String yellow_vip_level = (String) JSONObject.fromObject(backData3).get("yellow_vip_level");
//        System.out.println(yellow_vip_level);
//        String level = (String) JSONObject.fromObject(backData3).get("level");
//        System.out.println(level);
//        String is_yellow_year_vip = (String) JSONObject.fromObject(backData3).get("is_yellow_year_vip");
//        System.out.println(is_yellow_year_vip);

}


//发送get请求
public static String sendGetRequest(String url, String charset, int timeout) {
String result = "";
try {
URL u = new URL(url);
try {
URLConnection conn = u.openConnection();
conn.connect();
conn.setConnectTimeout(timeout);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));
String line = "";
while ((line = in.readLine()) != null) {

result = result + line;
}
in.close();
} catch (IOException e) {
return result;
}
} catch (MalformedURLException e) {
return result;
}

return result;
}
//发送post请求
public static String post(String url, Map<String, String> paramsMap) throws IOException {
CloseableHttpClient client = HttpClients.createDefault();
String responseText = "";
CloseableHttpResponse response = null;
try {
HttpPost method = new HttpPost(url);
if (paramsMap != null) {
List<NameValuePair> paramList = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> param : paramsMap.entrySet()) {
NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue());
paramList.add(pair);
}
method.setEntity(new UrlEncodedFormEntity(paramList, ENCODING));
}
response = client.execute(method);
HttpEntity entity = response.getEntity();
if (entity != null) {
responseText = EntityUtils.toString(entity);
}
} catch (Exception e) {
throw new IOException(e.getMessage());
} finally {
try {
response.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return responseText;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  第三方登录