公众号第三方平台开发 教程三 微信公众号授权第三方平台
发布时间:2017/12/23 15:13:19 来源:搜数网络 浏览:14

公众号第三方平台开发 教程一 创建公众号第三方平台

公众号第三方平台开发 教程二 component_verify_ticket和accessToken的获取

公众号第三方平台开发 教程三 微信公众号授权第三方平台

公众号第三方平台开发 教程四 代公众号发起网页授权说明

公众号第三方平台开发 教程五 代公众号处理消息和事件

公众号第三方平台开发 教程六 代公众号使用JS SDK说明



这一部分挺简单的,其实就是在页面上放一个链接,引导用户跳转即可


链接的格式如下:

https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=xxxx&pre_auth_code=xxxxx&redirect_uri=xxxx

这里对参数做一下说明

component_appid即第三方平台的APPID(如下图)

pre_auth_code为预授权码,用获得的第三方平台AccessToken作为参数进行获取,具体看下面的代码

redirect_uri为用户同意授权之后跳转的链接,同时会向该页面发送用户的授权码auth_code,通过这个授权码即可请求到公众号的授权信息

获取预授权码的函数

复制代码
        /// <summary> /// 用于获取预授权码。预授权码用于公众号授权时的第三方平台方安全验证 /// </summary> /// <param name="component_verify_ticket"></param> /// <returns></returns> public static ResponseCreatePreauthCode Create_preauthcode( string component_access_token)        
{ var urlFormat =  "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token={0}";  object data = null;
            data = new {
                component_appid = Config.ServerAppID,
            }; return CommonJsonSend.Send<ResponseCreatePreauthCode>(
component_access_token, urlFormat, data, timeOut: Config.TIME_OUT);
        }

 

 返回结果示例

{
"pre_auth_code":"Cx_Dk6qiBE0Dmx4EmlT3oRfArPvwSQ-oa3NL_fwHM7VI08r52wazoZX2Rhpz1dEw",
"expires_in":600
}

 

获得用户授权码之后,用auth_Code作为参数请求公众号信息。

component_access_token为第三方平台accessToken 

复制代码
        /// <summary> /// 使用授权码换取公众号的授权信息 /// </summary>  /// <param name="component_access_token"></param> /// <returns></returns> public static PublicWechatAuthorizerInfo Query_auth( string component_access_token, string auth_code_value)
        { var urlFormat =  "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token={0}";
 object data = null;
            data = new {
                component_appid = Config.ServerAppID,
                authorization_code = auth_code_value
            }; return CommonJsonSend.Send<PublicWechatAuthorizerInfo>(
component_access_token, urlFormat, data, timeOut: Config.TIME_OUT);
        }

 返回结果示例


"authorization_info": {
"authorizer_appid": "wxf8b4f85f3a794e77", 
"authorizer_access_token": "QXjUqNqfYVH0yBE1iI_7vuN_9gQbpjfK7hYwJ3P7xOa88a89-Aga5x1NMYJyB8G2yKt1KCl0nPC3W9GJzw0Zzq_dBxc8pxIGUNi_bFes0qM", 
"expires_in": 7200, 
"authorizer_refresh_token": "dTo-YCXPL4llX-u1W1pPpnp8Hgm4wpJtlR6iV0doKdY", 
"func_info": [
{
"funcscope_category": {
"id": 1
}
}, 
{
"funcscope_category": {
"id": 2
}
}, 
{
"funcscope_category": {
"id": 3
}
}
]
}

返回