Example #1
0
        static void Main()
        {
            UriBuilderImprove uriBuilder = new UriBuilderImprove("http://www.baidu.com");

            uriBuilder.Path = "s";
            uriBuilder.QueryString["wd"] = "小米";
            System.Console.WriteLine(HttpClient.getResponse(uriBuilder.ToString(), "GET", null));//测试httpclient能正常返回界面
            long             clientId = 179887661252608;
            String           clientKey = "50S2mk2MTDHQFbV6O6kMjg==", clientRedirectUri = "http://xiaomi.com";
            XiaoMiHttpClient httpClient = new XiaoMiHttpClient(clientId, clientKey);

            /**
             * getAuthorizeURL获得权限的uri
             * @param redirect_uri返回uri
             * @param response_type返回类型填token或code
             * @param scope 权限,没有为null
             * @param state 可选传参,没有为null
             * @param skip_confirm是否为黄页,默认为false
             * @return 权限的uri
             */
            string getTokenURI = httpClient.getAuthorizeURL(clientRedirectUri, "token", null, null, false);

            System.Console.WriteLine(getTokenURI);
            System.Console.WriteLine(HttpClient.getResponse(getTokenURI, "GET", null));//能正常返回界面
            string refresh_token = "eJxjYGAQSS_9M8X40acUv_lnpTc3sjO8O5eRzMDAwMgQDyQZ0pakngLRwTeSwDSjvaghA8Pi2TFqIB4Du6GCkYKxggmQyVuUmlaUWpwRX5KfnZoHALQ_GGY";

            /**
             * getRefreshTokenURL为刷新token的uri
             * @param redirect_uri返回uri
             * @param  grant_type使用类型填refresh_token或authorization_code
             * @param  parameter为使用的类型的值
             * @return 得到的token
             */
            string getRefreshTokenURI = httpClient.getRefreshTokenURL(clientRedirectUri, "refresh_token", refresh_token);

            System.Console.WriteLine(getRefreshTokenURI);
            string HTTP    = "GET";
            string URI     = "/user/relation";
            string mac_key = "b22OmhVgdRIVEPSWAeCyWaEA7GA";
            string token   = "eJxjYGAQqVSyKaqq1FK_NFfUU2Yb99pj4uoXGBgYGBnigSRDiHOWBYgOPnMYTDPaixoyMCyeHaMG4jGwGyoYKRgrmACZzLmJyQDggRAr";
            string QUERY   = "clientId=" + clientId + "&token=" + token;
            XiaoMiApiHttpClient apiHttpClient = new XiaoMiApiHttpClient(clientId, clientKey);

            /**
             * apiHttpClient.apiCall为api操作得到用户信息等
             * @param  method http方法,填“GET”或“POST”
             * @param  local_path使用参数详情看sdk文档
             * @param  mac_key,hmacsha1的密钥
             * @param  access_token访问令牌
             * @return 用户信息
             */
            string apiResponse = apiHttpClient.apiCall(HTTP, URI, mac_key, token);

            System.Console.WriteLine(apiResponse);
            System.Console.WriteLine(apiResponse.IndexOf("成功") != -1);
        }
 /**
 * HMACSHA1加密后为api操作得到用户信息等
 * @param  method http方法,填“GET”或“POST”
 * @param  localPath使用参数详情看sdk文档
 * @param  macKey,hmacsha1的密钥
 * @param  accessToken访问令牌
 * @return 用户信息
 */
 public String apiCall(string method, string localPath, string macKey, string accessToken)
 {
     UriBuilderImprove urlBuilder = new UriBuilderImprove(XiaoMiHttpClientConst.apiURI);
     urlBuilder.Path = localPath;
     urlBuilder.QueryString["clientId"] = clientId.ToString();
     urlBuilder.QueryString["token"] = accessToken;
     urlBuilder.ToString();
     method = method.ToUpperInvariant();
     string nonce = XiaoMiHttpClientUtils.getNonce();
     string query = urlBuilder.Query.Substring(1);
     Console.WriteLine(XiaoMiHttpClientConst.apiURI + localPath + "?" + query);
     string ciphertext = XiaoMiHttpClientUtils.createCiphertext(nonce, query, method, localPath, macKey);
     string header = XiaoMiHttpClientUtils.createHeader(nonce, ciphertext, accessToken);
     //http传输过程
     Dictionary<string, string> headermap = new Dictionary<string, string>();
     headermap.Add(XiaoMiHttpClientConst.authorizationName, header);
     string respHtml = HttpClient.getResponse(urlBuilder.ToString(), method, headermap);
     return respHtml;
 }
 /**
  * 获得权限的uri
  * @param redirectUri返回uri
  * @param responseType返回类型填token或code
  * @param scope 权限,没有为null
  * @param state 可选传参,没有为null
  * @param skipConfirm是否为黄页,默认为false
  * @return 权限的uri
  */
 public String getAuthorizeURL(String redirectUri, String responseType, String scope, String state, bool skipConfirm)
 {
     UriBuilderImprove urlBuilder = new UriBuilderImprove(XiaoMiHttpClientConst.authorizeURI);
     urlBuilder.QueryString[XiaoMiHttpClientConst.clientIdName] = clientId.ToString();
     urlBuilder.QueryString[XiaoMiHttpClientConst.redirectUriName] = redirectUri;
     urlBuilder.QueryString[XiaoMiHttpClientConst.responseTypeName] = responseType;
     if (scope != null) urlBuilder.QueryString[XiaoMiHttpClientConst.scopeeName] = scope;
     if (state != null) urlBuilder.QueryString[XiaoMiHttpClientConst.stateName] = state;
     if (skipConfirm) urlBuilder.QueryString[XiaoMiHttpClientConst.skipConfirmName] = skipConfirm.ToString();
     return  urlBuilder.ToString();
 }
Example #4
0
        /**
         * HMACSHA1加密后为api操作得到用户信息等
         * @param  method http方法,填“GET”或“POST”
         * @param  localPath使用参数详情看sdk文档
         * @param  macKey,hmacsha1的密钥
         * @param  accessToken访问令牌
         * @return 用户信息
         */
        public String apiCall(string method, string localPath, string macKey, string accessToken)
        {
            UriBuilderImprove urlBuilder = new UriBuilderImprove(XiaoMiHttpClientConst.apiURI);

            urlBuilder.Path = localPath;
            urlBuilder.QueryString["clientId"] = clientId.ToString();
            urlBuilder.QueryString["token"]    = accessToken;
            urlBuilder.ToString();
            method = method.ToUpperInvariant();
            string nonce = XiaoMiHttpClientUtils.getNonce();
            string query = urlBuilder.Query.Substring(1);

            Console.WriteLine(XiaoMiHttpClientConst.apiURI + localPath + "?" + query);
            string ciphertext = XiaoMiHttpClientUtils.createCiphertext(nonce, query, method, localPath, macKey);
            string header     = XiaoMiHttpClientUtils.createHeader(nonce, ciphertext, accessToken);
            //http传输过程
            Dictionary <string, string> headermap = new Dictionary <string, string>();

            headermap.Add(XiaoMiHttpClientConst.authorizationName, header);
            string respHtml = HttpClient.getResponse(urlBuilder.ToString(), method, headermap);

            return(respHtml);
        }
 static void Main()
 {
     UriBuilderImprove uriBuilder = new UriBuilderImprove("http://www.baidu.com");
     uriBuilder.Path = "s";
     uriBuilder.QueryString["wd"] = "小米";
     System.Console.WriteLine(HttpClient.getResponse(uriBuilder.ToString(), "GET", null));//测试httpclient能正常返回界面
     long clientId = 179887661252608;
     String clientKey = "50S2mk2MTDHQFbV6O6kMjg==", clientRedirectUri = "http://xiaomi.com";
     XiaoMiHttpClient httpClient = new XiaoMiHttpClient(clientId, clientKey);
     /**
      * getAuthorizeURL获得权限的uri
      * @param redirect_uri返回uri
      * @param response_type返回类型填token或code
      * @param scope 权限,没有为null
      * @param state 可选传参,没有为null
      * @param skip_confirm是否为黄页,默认为false
      * @return 权限的uri
      */
     string getTokenURI = httpClient.getAuthorizeURL(clientRedirectUri, "token", null, null, false);
     System.Console.WriteLine(getTokenURI);
     System.Console.WriteLine(HttpClient.getResponse(getTokenURI, "GET", null));//能正常返回界面
     string refresh_token="eJxjYGAQSS_9M8X40acUv_lnpTc3sjO8O5eRzMDAwMgQDyQZ0pakngLRwTeSwDSjvaghA8Pi2TFqIB4Du6GCkYKxggmQyVuUmlaUWpwRX5KfnZoHALQ_GGY";
     /**
      * getRefreshTokenURL为刷新token的uri
      * @param redirect_uri返回uri
      * @param  grant_type使用类型填refresh_token或authorization_code
      * @param  parameter为使用的类型的值
      * @return 得到的token
      */
     string getRefreshTokenURI = httpClient.getRefreshTokenURL(clientRedirectUri, "refresh_token", refresh_token);
     System.Console.WriteLine(getRefreshTokenURI);
     string HTTP="GET";
     string URI = "/user/relation";
     string mac_key = "b22OmhVgdRIVEPSWAeCyWaEA7GA";
     string token = "eJxjYGAQqVSyKaqq1FK_NFfUU2Yb99pj4uoXGBgYGBnigSRDiHOWBYgOPnMYTDPaixoyMCyeHaMG4jGwGyoYKRgrmACZzLmJyQDggRAr";
     string QUERY = "clientId=" + clientId + "&token=" + token;
     XiaoMiApiHttpClient apiHttpClient = new XiaoMiApiHttpClient(clientId, clientKey);
     /**
      * apiHttpClient.apiCall为api操作得到用户信息等
      * @param  method http方法,填“GET”或“POST”
      * @param  local_path使用参数详情看sdk文档
      * @param  mac_key,hmacsha1的密钥
      * @param  access_token访问令牌
      * @return 用户信息
      */
     string apiResponse = apiHttpClient.apiCall(HTTP, URI, mac_key, token);
     System.Console.WriteLine(apiResponse);
     System.Console.WriteLine(apiResponse.IndexOf("成功") != -1);
 }
 /**
  * 刷新token
  * @param  redirectUri返回uri
  * @param  grantType使用类型填refresh_token或authorization_code
  * @param  value为使用的类型的值
  * @return 得到的token
  */
 public String getRefreshTokenURL(String redirectUri, String grantType, String val)
 {
     UriBuilderImprove urlBuilder = new UriBuilderImprove(XiaoMiHttpClientConst.tokenURI);
     urlBuilder.QueryString[XiaoMiHttpClientConst.clientIdName] = clientId.ToString();
     urlBuilder.QueryString[XiaoMiHttpClientConst.redirectUriName] = redirectUri;
     urlBuilder.QueryString[XiaoMiHttpClientConst.clientSecretName] = clientKey;
     if (grantType.Equals(XiaoMiHttpClientConst.refreshTokenName))
     {
         urlBuilder.QueryString[XiaoMiHttpClientConst.grantTypeName] = XiaoMiHttpClientConst.refreshTokenName;
         urlBuilder.QueryString[XiaoMiHttpClientConst.refreshTokenName] =  val;
     }
     else
     {
         urlBuilder.QueryString[XiaoMiHttpClientConst.grantTypeName] = XiaoMiHttpClientConst.authorizationCodeName;
         urlBuilder.QueryString[XiaoMiHttpClientConst.codeName] = val;
     }
     return urlBuilder.ToString();
 }
        /**
         * 刷新token
         * @param  redirectUri返回uri
         * @param  grantType使用类型填refresh_token或authorization_code
         * @param  value为使用的类型的值
         * @return 得到的token
         */
        public String getRefreshTokenURL(String redirectUri, String grantType, String val)
        {
            UriBuilderImprove urlBuilder = new UriBuilderImprove(XiaoMiHttpClientConst.tokenURI);

            urlBuilder.QueryString[XiaoMiHttpClientConst.clientIdName]     = clientId.ToString();
            urlBuilder.QueryString[XiaoMiHttpClientConst.redirectUriName]  = redirectUri;
            urlBuilder.QueryString[XiaoMiHttpClientConst.clientSecretName] = clientKey;
            if (grantType.Equals(XiaoMiHttpClientConst.refreshTokenName))
            {
                urlBuilder.QueryString[XiaoMiHttpClientConst.grantTypeName]    = XiaoMiHttpClientConst.refreshTokenName;
                urlBuilder.QueryString[XiaoMiHttpClientConst.refreshTokenName] = val;
            }
            else
            {
                urlBuilder.QueryString[XiaoMiHttpClientConst.grantTypeName] = XiaoMiHttpClientConst.authorizationCodeName;
                urlBuilder.QueryString[XiaoMiHttpClientConst.codeName]      = val;
            }
            return(urlBuilder.ToString());
        }
        /**
         * 获得权限的uri
         * @param redirectUri返回uri
         * @param responseType返回类型填token或code
         * @param scope 权限,没有为null
         * @param state 可选传参,没有为null
         * @param skipConfirm是否为黄页,默认为false
         * @return 权限的uri
         */
        public String getAuthorizeURL(String redirectUri, String responseType, String scope, String state, bool skipConfirm)
        {
            UriBuilderImprove urlBuilder = new UriBuilderImprove(XiaoMiHttpClientConst.authorizeURI);

            urlBuilder.QueryString[XiaoMiHttpClientConst.clientIdName]     = clientId.ToString();
            urlBuilder.QueryString[XiaoMiHttpClientConst.redirectUriName]  = redirectUri;
            urlBuilder.QueryString[XiaoMiHttpClientConst.responseTypeName] = responseType;
            if (scope != null)
            {
                urlBuilder.QueryString[XiaoMiHttpClientConst.scopeeName] = scope;
            }
            if (state != null)
            {
                urlBuilder.QueryString[XiaoMiHttpClientConst.stateName] = state;
            }
            if (skipConfirm)
            {
                urlBuilder.QueryString[XiaoMiHttpClientConst.skipConfirmName] = skipConfirm.ToString();
            }
            return(urlBuilder.ToString());
        }