http://mp.weixin.qq.com/wiki這個是官網的接口文檔
微信授權獲取用戶openid-JAVA
開發微信測試需要用到的代碼和jar包都在里面 包括核心代碼 上面圖片可以掃一掃 你懂得
鏈接: http://mobile.qzone.qq.com/l?g=1357&sharekey=e43cb6432868c4b709c5351c3c82d3de
/**
* 網頁授權獲取openId第2步,根據code取得openId
*
* @param appid 公眾號的唯一標識
* @param secret 公眾號的appsecret密鑰
* @param code code為換取access_token的票據
* @return
*/
代碼為OAuthInfo 不在此博客中 OAuthInfo只獲取到Openid 。獲取詳細信息。繼續調用微信接口
拉取用戶信息(需scope為 snsapi_userinfo)
如果網頁授權作用域為snsapi_userinfo,則此時開發者可以通過access_token和openid拉取用戶信息了。
請求方法
http:GET(請使用https協議) https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
第一步:用戶同意授權,獲取code
在確保微信公眾賬號擁有授權作用域(scope參數)的權限的前提下(服務號獲得高級接口后,默認帶有scope參數中的snsapi_base和snsapi_userinfo),引導關注者打開如下頁面:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
我的代碼如下:GetWeiXinCode
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
|
package
cn.elve.jxt.weixin.weixin.util;
import
java.net.URLEncoder;
import
cn.elve.jxt.weixin.util.Constants;
/**
* 獲取微信的code
* @author 宗瀟帥
* @修改日期 2014-7-21下午1:01:45
*/
public
class
GetWeiXinCode {
public
static
String GetCodeRequest =
"https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect"
;
public
static
String getCodeRequest(){
String result =
null
;
GetCodeRequest = GetCodeRequest.replace(
"APPID"
, urlEnodeUTF8(Constants.appId));
GetCodeRequest = GetCodeRequest.replace(
"REDIRECT_URI"
,urlEnodeUTF8(Constants.REDIRECT_URI));
GetCodeRequest = GetCodeRequest.replace(
"SCOPE"
, Constants.SCOPE);
result = GetCodeRequest;
return
result;
}
public
static
String urlEnodeUTF8(String str){
String result = str;
try
{
result = URLEncoder.encode(str,
"UTF-8"
);
}
catch
(Exception e) {
e.printStackTrace();
}
return
result;
}
public
static
void
main(String[] args) {
System.out.println(getCodeRequest());
}
}
|
替換相應的APPID APPSECRET SCOPE
第二步:通過code換取網頁授權access_token
獲取code后,請求以下鏈接獲取access_token: https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
具體做法與上面基本一致。更換相對應的值。需要注意的是code可以寫一個Servlet獲取。String code = request.getParameter("code");get/post都可以。
這樣子就會返回一下json格式數據
{ "access_token":"ACCESS_TOKEN", "expires_in":7200, "refresh_token":"REFRESH_TOKEN", "openid":"OPENID", "scope":"SCOPE" }
我們需要對這個json格式數據進行轉換。大寫自己都是用自己賬號獲取的數據。所有做了替換。
oAuthInfo = new OAuthInfo();網頁授權接口調用憑證5個參數
oAuthInfo.setAccessToken(jsonObject.getString("access_token"));
oAuthInfo.setExpiresIn(jsonObject.getInt("expires_in"));
oAuthInfo.setRefreshToken(jsonObject.getString("refresh_token"));
oAuthInfo.setOpenId(jsonObject.getString("openid"));
oAuthInfo.setScope(jsonObject.getString("scope"));
根據json。獲取key,就能得到value。
這就獲取到用戶的openid。應用授權作用域,snsapi_base (不彈出授權頁面,直接跳轉,只能獲取用戶openid),snsapi_userinfo (彈出授權頁面,可通過openid拿到昵稱、性別、所在地。並且,即使在未關注的情況下,只要用戶授權,也能獲取其信息)我自己用的作用域為snsapi_userinfo。用戶點擊跳轉頁面為
寫一個Servlet專門接收傳遞過來的code。進行相應的操作。
1.OAuthServlet 對code進行access——token的驗證
2.GetWeiXinCode的方法調用接口地址。得到相應code。
3.OAuthInfo 返回數據相應的參數的PO類。set/get方法
4.WeiXinUtil添加一個方法 publicOAuth getOAuthInfo(String appid, String secret, String code)得到json格式。並使用JSONObject讀取出自己想要的數據。
個人微博 http://weibo.com/u/2205636212