微信登陆接口

参数

参数

说明

appid

应用唯一标识,在微信开放平台提交应用审核通过后获得

scope

应用授权作用域,如获取用户个人信息则填写 snsapi_userinfo(什么是授权域?)

state

用户保持请求和回调的状态,授权请求后原样待会给第三方。该参数可用于防止csrf攻击()跨站请求伪造攻击,建议第三方带上该参数,可设置为简单的随机数加session进行校验。

code

通过第一步获取的code参数

grant_type

填写 authorization_code

access_token

接口调用凭证

expires_in

access_token接口调用凭证超时时间 单位秒

refresh_token

用户刷新access_token

openid

授权用户唯一标识

scope

用户授权的作用域,使用逗号(,)分隔

unionid

只有在用户将公众好绑定到微信开放平台账号后,才会出现该字段。

  1. 调用微信登录接口,用户扫码登录,生成code

    https://open.weixin.qq.com/connect/qrconnect?
    appid=xxxxxxxxxxxxxx
    &redirect_uri=https://baidu.com
    &response_type=code
    &scope=snsapi_login
    &state=200

     

  2. 请求以下路径通过code获取access_token 参数:appid=APPID,secret=SECRET,code=填写上一步生成的code,grant_type=“authorization_code”,生成access_token

    https://api.weixin.qq.com/sns/oauth2/access_token?
    appid=xxxxxxxxxxxxxx&
    secret=SECRETyyyyyyyyyyyyyyy&
    code=0610Xey313BXSQ15G6v31H6wy310Xeyv&
    grant_type=authorization_code

    返回参数

    结果:
    { 
    "access_token":"ACCESS_TOKEN",                       	接口调用凭证
    "expires_in":7200, 			          	access_token接口调用凭证超时时间,单位(秒)
    "refresh_token":"REFRESH_TOKEN",		用户刷新access_token
    "openid":"OPENID", 				授权用户唯一标识
    "scope":"SCOPE",				用户授权的作用域,使用逗号(,)分隔
    "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"	当且仅当该网站应用已获得该用户的userinfo授权时,才会出现该字段。
    }

     

  3. 请求以下链接进行refresh_token 参数:appid=APPID,grant_type=‘refresh_token’,refresh_token=填写上一步获取到的refresh_token参数

    https://api.weixin.qq.com/sns/oauth2/refresh_token?
    appid=APPID&
    grant_type=refresh_token&
    refresh_token=23_ivXL7QsljTjYUJaifpGajw6DaWu3N-2A5-GewcFpSEK_xZ_CSBGBJ35HpXS-qa4

    返回参数

    结果:
    { 
    "access_token":"ACCESS_TOKEN", 		接口调用凭证
    "expires_in":7200, 				access_token接口调用凭证超时时间,单位(秒)
    "refresh_token":"REFRESH_TOKEN", 		用户刷新access_token
    "openid":"OPENID", 				授权用户唯一标识
    "scope":"SCOPE" 				用户授权的作用域,使用逗号(,)分隔
    }

     

  4. 请求以下连接查询用户基本信息 参数:access_token=填写上一步获取的access_token,openid=填写上一步获取的openid

https://api.weixin.qq.com/sns/userinfo?access_token=access_token&openid=openid

返回参数

结果:
{"openid":"ooooooooooooo",
"nickname":"朝",
"sex":1,
"language":"zh_CN",
"city":"Hangzhou",
"province":"Zhejiang",
"country":"CN",
"headimgurl":"http:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/Q0j4TwGTfTIlicQP4zdmtiaDkLEus7e8GMHZXnnD2YqLWpgEPl5QGPAKe02x4nMwZricqBOzo8gF0mm3gLyL0zh2Q\/132",
"privilege":[],
"unionid":"oniwU5vxax7qU27Xdy3Cb-MYvYeE"}

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章