微信受權登陸

第一步:用戶贊成受權,獲取code 

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5b6e96c8c89c614e&redirect_uri=http://s14k870241.iask.in/index.html&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect
#返回url:http://s14k870241.iask.in/index.html?code=001OWsKC0kQWLc2tpINC0j7lKC0OWsKC&state=STATE


第二步:經過code換取網頁受權access_token

#code只能調一次該接口,且成功後原來的access_token會失效
https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx5b6e96c8c89c614e&secret=xxxxxxxxx&code=001OWsKC0kQWLc2tpINC0j7lKC0OWsKC&grant_type=authorization_code
返回JSON:
{
    "access_token": "5_pEk49UifMKmep3FScEy5XCuAaITXE4wOEXYstu_4sFqY2B0S280ujUs1ZwzHE-iBTVUNQj33BZWlH0WE0k9kFQ",
    "expires_in": 7200,
    "refresh_token": "5_BqSz9Og718SV8igq-esLP2jYvl-_e_GIqH-ti8eAwOutRyylX1zIfjOUx_VJFlftJfEO8Md_J2q5PT7pJOrOBA",
    "openid": "ob8F-wFENFWw7_FR6Gx8KbPacFEw",
    "scope": "snsapi_base",
    "unionid": "oTpR9xAIiebFOc6p04tMTaLhviCQ"
}

第三步:刷新access_token

#(若是須要)因爲access_token擁有較短的有效期,當access_token超時後,能夠使用refresh_token進行刷新,refresh_token有效期爲30天,當refresh_token失效以後,須要用戶從新受權。
https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=wx5b6e96c8c89c614e&grant_type=refresh_token&refresh_token=5_BqSz9Og718SV8igq-esLP2jYvl-_e_GIqH-ti8eAwOutRyylX1zIfjOUx_VJFlftJfEO8Md_J2q5PT7pJOrOBA
返回JSON:
{
    "openid": "ob8F-wFENFWw7_FR6Gx8KbPacFEw",
    "access_token": "5_pEk49UifMKmep3FScEy5XCuAaITXE4wOEXYstu_4sFqY2B0S280ujUs1ZwzHE-iBTVUNQj33BZWlH0WE0k9kFQ",
    "expires_in": 7200,
    "refresh_token": "5_BqSz9Og718SV8igq-esLP2jYvl-_e_GIqH-ti8eAwOutRyylX1zIfjOUx_VJFlftJfEO8Md_J2q5PT7pJOrOBA",
    "scope": "snsapi_base,snsapi_userinfo,"
}

第四步:拉取用戶信息(需scope爲 snsapi_userinfo)

https://api.weixin.qq.com/sns/userinfo?access_token=5_pEk49UifMKmep3FScEy5XCuAaITXE4wOEXYstu_4sFqY2B0S280ujUs1ZwzHE-iBTVUNQj33BZWlH0WE0k9kFQ&openid=ob8F-wFENFWw7_FR6Gx8KbPacFEw&lang=zh_CN
返回JSON:
{
    "openid": "ob8F-wFENFWw7_FR6Gx8KbPacFEw",
    "nickname": "李玉長",
    "sex": 1,
    "language": "zh_CN",
    "city": "懷化",
    "province": "湖南",
    "country": "中國",
    "headimgurl": "http://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJ6xHJunSibZQaQV3SvLfJtluniatYJ1s0IvPpLq06frM6X8MNbgicPMdKoL2jiaehmnOp8aPwG0Io6pQ/132",
    "privilege": [],
    "unionid": "oTpR9xAIiebFOc6p04tMTaLhviCQ"
}

思考:整個應用其實只需用戶手動點一次微信登陸受權便可,之後均可以用snsapi_base方式靜默跳轉只獲取openid實現登陸。html

相關文章
相關標籤/搜索