- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Java微信公众号开发之通过微信公众号获取用户信息由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
最近由于公司业务,就开始研究微信开发的流程,说实话,这东西刚开始看到时候和看天书的一样,总算,看了一天的文档,测试代码终于出来了.
1、首先需要到微信网站去设置一下,我是直接用的微信测试号.
接口配置信息必须要填写的,所以说必须能将自己的服务发布出去 。
到此微信配置完毕,接下来就是直接上代码了 。
2、获取用户信息的方式一共是两种,前提都是用户关注微信公众号,一种是静默获取(snsapi_base,这种方式只能获取openid),另一种是授权获取(snsapi_userinfo,可以获取用户的详细信息).
先说第一种 。
(1)首先需要先访问微信的链接 。
这里的 uri就是直接回掉我们的服务地址,一定要记住,服务校验的判断,我是按照来判断的echostr(第二种方式也是这样) 。
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
|
package
net.itraf.controller;
import
java.io.ioexception;
import
java.io.inputstream;
import
java.io.printwriter;
import
java.net.httpurlconnection;
import
java.net.malformedurlexception;
import
java.net.url;
import
javax.servlet.http.httpservletresponse;
import
org.springframework.stereotype.controller;
import
org.springframework.web.bind.annotation.requestmapping;
import
org.springframework.web.bind.annotation.responsebody;
import
com.alibaba.fastjson.jsonobject;
@controller
@requestmapping
(
"/open"
)
public
class
opencontroller {
@requestmapping
(
"/toopenid"
)
public
@responsebody
string getopenid(string code,string echostr,httpservletresponse res)
throws
ioexception{
if
(echostr==
null
){
string url=
"https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx24d47d2080f54c5b&secret=95011ac70909e8cca2786217dd80ee3f&code="
+code+
"&grant_type=authorization_code"
;
system.out.println(code);
string openid=
""
;
try
{
url geturl=
new
url(url);
httpurlconnection http=(httpurlconnection)geturl.openconnection();
http.setrequestmethod(
"get"
);
http.setrequestproperty(
"content-type"
,
"application/x-www-form-urlencoded"
);
http.setdooutput(
true
);
http.setdoinput(
true
);
http.connect();
inputstream is = http.getinputstream();
int
size = is.available();
byte
[] b =
new
byte
[size];
is.read(b);
string message =
new
string(b,
"utf-8"
);
jsonobject json = jsonobject.parseobject(message);
openid = json.getstring(
"openid"
);
}
catch
(malformedurlexception e) {
e.printstacktrace();
}
catch
(ioexception e) {
e.printstacktrace();
}
return
openid;
}
else
{
printwriter out = res.getwriter();
out.print(echostr);
return
null
;
}
}
//做服务器校验
@requestmapping
(
"/tovalid"
)
public
void
valid(string echostr,httpservletresponse res)
throws
ioexception{
printwriter out = res.getwriter();
out.print(echostr);
}
}
|
第二种 。
(1) 。
域名 。
/open/openid&response_type=code&scope=snsapi_userinfo&state=1&connect_redirect=1#wechat_redirect 。
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
|
package
net.itraf.controller;
import
java.io.ioexception;
import
java.io.printwriter;
import
javax.servlet.servletexception;
import
javax.servlet.http.httpservletrequest;
import
javax.servlet.http.httpservletresponse;
import
net.sf.json.jsonobject;
import
org.springframework.stereotype.controller;
import
org.springframework.web.bind.annotation.requestmapping;
@controller
@requestmapping
(
"/weixin"
)
public
class
oauth2action {
@requestmapping
(
"/oauth"
)
public
void
auth(httpservletrequest request, httpservletresponse response)
throws
servletexception, ioexception {
string echostr = request.getparameter(
"echostr"
);
if
(echostr==
null
){
string appid =
"wx24d47d2080f54c5b"
;
string appsecret =
"95011ac70909e8cca2786217dd80ee3f"
;
//拼接
string get_access_token_url =
"https://api.weixin.qq.com/sns/oauth2/access_token?"
+
"appid="
+ appid
+
"&secret="
+ appsecret
+
"&code=code&grant_type=authorization_code"
;
string get_userinfo =
"https://api.weixin.qq.com/sns/userinfo?access_token=access_token&openid=openid&lang=zh_cn"
;
request.setcharacterencoding(
"utf-8"
);
response.setcharacterencoding(
"utf-8"
);
string code = request.getparameter(
"code"
);
system.out.println(
"******************code="
+ code);
get_access_token_url = get_access_token_url.replace(
"code"
, code);
string json = httpsgetutil.dohttpsgetjson(get_access_token_url);
jsonobject jsonobject = jsonobject.fromobject(json);
string access_token = jsonobject.getstring(
"access_token"
);
string openid = jsonobject.getstring(
"openid"
);
get_userinfo = get_userinfo.replace(
"access_token"
, access_token);
get_userinfo = get_userinfo.replace(
"openid"
, openid);
string userinfojson = httpsgetutil.dohttpsgetjson(get_userinfo);
jsonobject userinfojo = jsonobject.fromobject(userinfojson);
string user_openid = userinfojo.getstring(
"openid"
);
string user_nickname = userinfojo.getstring(
"nickname"
);
string user_sex = userinfojo.getstring(
"sex"
);
string user_province = userinfojo.getstring(
"province"
);
string user_city = userinfojo.getstring(
"city"
);
string user_country = userinfojo.getstring(
"country"
);
string user_headimgurl = userinfojo.getstring(
"headimgurl"
);
response.setcontenttype(
"text/html; charset=utf-8"
);
printwriter out = response.getwriter();
out.println(
"<!doctype html public \"-//w3c//dtd html 4.01 transitional//en\">"
);
out.println(
"<html>"
);
out.println(
" <head><title>a servlet</title></head>"
);
out.println(
" <body>"
);
out.print(
" this is "
);
out.print(
this
.getclass());
out.println(
", using the post method \n"
);
out.println(
"openid:"
+ user_openid +
"\n\n"
);
out.println(
"nickname:"
+ user_nickname +
"\n\n"
);
out.println(
"sex:"
+ user_sex +
"\n\n"
);
out.println(
"province:"
+ user_province +
"\n\n"
);
out.println(
"city:"
+ user_city +
"\n\n"
);
out.println(
"country:"
+ user_country +
"\n\n"
);
out.println(
"<img src=/"
+ user_headimgurl +
"/"
);
out.println(
">"
);
out.println(
" </body>"
);
out.println(
"</html>"
);
out.flush();
out.close();
}
else
{
printwriter out = response.getwriter();
out.print(echostr);
}
}
}
|
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
|
package
net.itraf.controller;
import
java.io.ioexception;
import
java.io.inputstream;
import
java.net.httpurlconnection;
import
java.net.malformedurlexception;
import
java.net.url;
public
class
httpsgetutil {
public
static
string dohttpsgetjson(string url)
{
string message =
""
;
try
{
system.out.println(
"dohttpsgetjson"
);
//todo:dd
url urlget =
new
url(url);
httpurlconnection http = (httpurlconnection) urlget.openconnection();
http.setrequestmethod(
"get"
);
//必须是get方式请求 24
http.setrequestproperty(
"content-type"
,
"application/x-www-form-urlencoded"
);
http.setdooutput(
true
);
http.setdoinput(
true
);
system.setproperty(
"sun.net.client.defaultconnecttimeout"
,
"30000"
);
//连接超时30秒28
system.setproperty(
"sun.net.client.defaultreadtimeout"
,
"30000"
);
//读取超时30秒29 30
http.connect();
inputstream is =http.getinputstream();
int
size =is.available();
byte
[] jsonbytes =
new
byte
[size];
is.read(jsonbytes);
message=
new
string(jsonbytes,
"utf-8"
);
}
catch
(malformedurlexception e)
{
e.printstacktrace();
}
catch
(ioexception e)
{
e.printstacktrace();
}
return
message;
}
}
|
以上所述是小编给大家介绍的java微信公众号开发之通过微信公众号获取用户信息,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我网站的支持! 。
原文链接:http://www.cnblogs.com/yangming5423/archive/2017/05/18/6873267.html 。
最后此篇关于Java微信公众号开发之通过微信公众号获取用户信息的文章就讲到这里了,如果你想了解更多关于Java微信公众号开发之通过微信公众号获取用户信息的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
在为 Web 应用程序用例图建模时,为用户可以拥有的每个角色创建一个角色是否更好?或拥有一个角色、用户和一个具有特权的矩阵? guest < 用户 < 版主 < 管理员 1: guest 、用户、版主
我无法使用 Elixir 连接到 Postgres: ** (Mix) The database for PhoenixChat.Repo couldn't be created: FATAL 28P
这个问题已经有答案了: Group by field name in Java (7 个回答) 已关闭 7 年前。 我必须编写一个需要 List 的方法并返回 Map> . User包含 Person
感谢您的帮助,首先我将显示代码: $dotaz = "Select * from customers JOIN contracts where customers.user_id ='".$_SESS
我只想向所有用户中的一个用户显示一个按钮。我尝试了 orderByKey() 但没有成功! 用户模型有 id 成员,我尝试使用 orderByChild("id") 但结果相同! 我什至尝试了以下技巧
我们在工作中从 MongoDB 切换到 Postgres,我正在建立一个 BDR 组。 在这一步,我正在考虑安全性并尽可能锁定。因此,我希望设置一个 replication 用户(角色)并让 BDR
export class UserListComponent implements OnInit{ users; constructor(private userService: UserS
我可以使用 Sonata User Bundle 将 FOS 包集成到 sonata Admin 包中。我的登录功能正常。现在我想添加 FOSUserBundle 中的更改密码等功能到 sonata
在 LinkedIn 中创建新应用程序时,我得到 4 个单独的代码: API key 秘钥 OAuth 用户 token OAuth 用户密码 我在 OAuth 流程中使用前两个。 的目的是什么?最后
所以..我几乎解决了所有问题。但现在我要处理另一个问题。我使用了这个连接字符串: SqlConnection con = new SqlConnection(@"Data Source=.\SQLEX
我有一组“用户”和一组“订单”。我想列出每个 user_id 的所有 order_id。 var users = { 0: { user_id: 111, us
我已经为我的Django应用创建了一个用户模型 class User(Model): """ The Authentication model. This contains the u
我被这个问题困住了,找不到解决方案。寻找一些方向。我正在用 laravel 开发一个新的项目,目前正致力于用户认证。我正在使用 Laravels 5.8 身份验证模块。 对密码恢复 View 做了一些
安装后我正在使用ansible配置几台计算机。 为此,我在机器上本地运行 ansible。安装中的“主要”用户通常具有不同的名称。我想将该用户用于诸如 become_user 之类的变量. “主要”用
我正在尝试制作一个运行 syncdb 的批处理文件来创建一个数据库文件,然后使用用户名“admin”和密码“admin”创建一个 super 用户。 到目前为止我的代码: python manage.
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 6 年前。 Improv
我已在 Azure 数据库服务器上设置异地复制。 服务器上运行的数据库之一具有我通过 SSMS 创建的登录名和用户: https://learn.microsoft.com/en-us/azure/s
我有一个 ionic 2 应用程序,正在使用 native FB Login 来检索名称/图片并将其保存到 NativeStorage。流程是我打开WelcomePage、登录并保存数据。从那里,na
这是我的用户身份验证方法: def user_login(request): if request.method == 'POST': username = request.P
我试图获取来自特定用户的所有推文,但是当我迭代在模板中抛出推文时,我得到“User”对象不可迭代 观看次数 tweets = User.objects.get(username__iexact='us
我是一名优秀的程序员,十分优秀!