zoukankan      html  css  js  c++  java
  • .net 后台如何模拟http请求获取微信接口的AccessToken(基础支持)

    1、首先你需要要公众号的key(Appid),value(AppSecret) ,这是公众号的唯一凭证

    1)登录公众号(服务号)

    2)开发-->基本设置-->公众号的Appid,AppSecret信息

     3)Appsecret需要自己重置后记住密码,页面不显示

    2、由于现在微信第三方接口比较严格,需要在公众号加上调用方的IP到白名单中

    1)开发-->基本设置-->IP白名单

    添加上调用方的IP地址(143.123.21.2),如:不加上IP地址,调用accesstoken接口会失败

     3、调用获取公众号基础支持的accessToken

    1)添加http请求的辅助方法

            /// <summary>
            /// 后台发送POST请求
            /// </summary>
            /// <param name="url">服务器地址</param>
            /// <param name="data">发送的数据</param>
            /// <returns></returns>
            public string HttpPost(string url, string data)
            {
                try
                {
                    //创建post请求
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    request.Method = "POST";
                    request.ContentType = "application/json;charset=UTF-8";
                    byte[] payload = Encoding.UTF8.GetBytes(data);
                    request.ContentLength = payload.Length;
    
                    //发送post的请求
                    Stream writer = request.GetRequestStream();
                    writer.Write(payload, 0, payload.Length);
                    writer.Close();
    
                    //接受返回来的数据
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream stream = response.GetResponseStream();
                    StreamReader reader = new StreamReader(stream, Encoding.UTF8);
                    string value = reader.ReadToEnd();
    
                    reader.Close();
                    stream.Close();
                    response.Close();
    
                    return value;
                }
                catch (Exception)
                {
                    return "";
                }
            }
    
            /// <summary>
            /// 后台发送GET请求
            /// </summary>
            /// <param name="url">服务器地址</param>
            /// <param name="data">发送的数据</param>
            /// <returns></returns>
            public string HttpGet(string url, string data)
            {
                try
                {
                    //创建Get请求
                    url = url + (data == "" ? "" : "?") + data;
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    request.Method = "GET";
                    request.ContentType = "text/html;charset=UTF-8";
    
                    //接受返回来的数据
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream stream = response.GetResponseStream();
                    StreamReader streamReader = new StreamReader(stream, Encoding.GetEncoding("utf-8"));
                    string retString = streamReader.ReadToEnd();
    
                    streamReader.Close();
                    stream.Close();
                    response.Close();
    
                    return retString;
                }
                catch (Exception)
                {
                    return "";
                }
            }

    2)调用获取的后台方法

            /// <summary>
            /// 获取基础支持的accessToken
            /// </summary>
            /// <param name="appid">公众号凭证ID</param>
            /// <param name="secret">公众号凭证密码</param>
            /// <returns></returns>
            public string GetBaseAcccessToken(string appid, string secret)
            {
                string accessToken = string.Empty;
    
                string reqUrl = string.Format(@"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appid, secret);
                string resultStr = HttpGet(reqUrl,"");
                dynamic data = JsonConvert.DeserializeObject(resultStr);
                if (data["access_token"] != null)
                {
                    accessToken = data["access_token"].ToString();
                }
                else
                {
                    accessToken = "";
                }
                return accessToken;
            }

    3)调用后返回的结果

    3.1)成功

    {
        "access_token": "35_4KxKJBhXFcRI7uM6FBixvZNDP7pPQuGK-AFsydgmGR3P4z9itSLvQlNmW8tYkOoOkGB_gb2P-VekacJnoMXghUwIRWQzSDMXyiIIouBtk06zPpQ0ddJ8O-5BjLM0PZArnJjsixN2kx1gcF_iJZDaAEADTX", 
        "expires_in": 7200
    }

    3.2)失败(未加入IP白名单)

    {
        "errcode": 40164, 
        "errmsg": "invalid ip 113.111.4.104, not in whitelist"
    }
  • 相关阅读:
    Codeforces Round #620 (Div. 2)
    Codeforces Round #575 (Div. 3)
    Codeforces Round #619 (Div. 2)
    2014 Nordic Collegiate Programming Contest
    Educational Codeforces Round 82 (Rated for Div. 2)
    模板
    2015-2016 ACM-ICPC Southwestern Europe Regional Contest (SWERC 15)
    模板
    Codeforces Round #618 (Div. 2)
    Codeforces Round #343 (Div. 2)
  • 原文地址:https://www.cnblogs.com/xielong/p/13427297.html
Copyright © 2011-2022 走看看