/// <summary> /// 获取token /// </summary> /// <param name="code"></param> /// <param name="state"></param> /// <returns></returns> public async Task <string> GetToken(string code, string state, string host) { string authCode = System.Convert.ToBase64String(Encoding.ASCII.GetBytes($"{ElemeOptions.Key}:{ElemeOptions.Secret}")); client.AddAuthorization("Basic", authCode); Dictionary <string, string> dic = new Dictionary <string, string>(); dic.Add("grant_type", "authorization_code"); dic.Add("code", code); string returnUrl = "http://" + host + "/api/elmpoi/authcallback"; //returnUrl = HttpUtility.UrlEncode(returnUrl); dic.Add("redirect_uri", returnUrl); dic.Add("client_id", ElemeOptions.Key); var res = await client.PostFormAsync(ElemeOptions.GetAuthTokenUrl(), dic); if (res.StatusCode == System.Net.HttpStatusCode.OK) { return(res.Result); } else { throw new Exception(res.ErrorMessage); } }
/// <summary> /// 获取授权地址 /// </summary> /// <param name="url"></param> /// <param name="poiId"></param> /// <returns></returns> public string GetAuthCode(string host, long poiId) { string returnUrl = "http://" + host + "/api/elmpoi/authcallback"; returnUrl = HttpUtility.HtmlEncode(returnUrl); return($"{ElemeOptions.GetAuthUrl()}?response_type=code&client_id={ElemeOptions.Key}&redirect_uri={returnUrl}&state={poiId}&scope=all"); }
/// <summary> /// 发起Post请求 /// </summary> /// <param name="request"></param> /// <param name="url"></param> /// <returns></returns> private async Task <dynamic> PostRequest() { request.SetSign(); string url = ElemeOptions.GetUrl(); var res = await client.PostJsonAsync(url, request); if (res.StatusCode == System.Net.HttpStatusCode.OK || res.StatusCode == 0) { return(ReturnResult(res)); } else { throw new Exception("发起饿了么请求失败,原因:" + res.ErrorMessage); } }
/// <summary> /// 刷新token /// </summary> /// <param name="refreshToken"></param> /// <returns></returns> public async Task <string> RefreshToken(string refreshToken) { string authCode = System.Convert.ToBase64String(Encoding.ASCII.GetBytes($"{ElemeOptions.Key}:{ElemeOptions.Secret}")); client.AddAuthorization("Basic", authCode); Dictionary <string, string> dic = new Dictionary <string, string>(); dic.Add("grant_type", "refresh_token"); dic.Add("refresh_token", refreshToken); var res = await client.PostFormAsync(ElemeOptions.GetAuthTokenUrl(), dic); if (res.StatusCode == System.Net.HttpStatusCode.OK) { return(res.Result); } else { throw new Exception(res.ErrorMessage); } }