/// <summary> /// 请求授权 /// </summary> /// <param name="Code">调用authorize获得的code值。</param> /// <returns>AccessTokenInfo</returns> public static async Task<AccessTokenInfo> GetAccessToken(string Code) { string url = "https://api.worktile.com/oauth2/access_token"; Dictionary<string, string> parameters = new Dictionary<string, string>(); parameters.Add("client_id", app_key); parameters.Add("client_secret", app_secret); parameters.Add("code", Code); string json = await Helper.HttpHelper.DoPost(url, parameters); if (string.IsNullOrEmpty(json)) { return null; } else { //开始解析返回数据 try { Debug.WriteLine("开始解析返回json---\n" + json); JObject result = JObject.Parse(json); Debug.WriteLine("解析返回json成功---\n"); AccessTokenInfo accessToken = new AccessTokenInfo(); accessToken.AccessToken = result["access_token"].ToString(); accessToken.RefreshToken = result["refresh_token"].ToString(); accessToken.RequestTime = DateTime.Now; int expiresIn = int.Parse(result["expires_in"].ToString()); accessToken.ExpiresTime = accessToken.RequestTime.AddSeconds(expiresIn); return accessToken; } catch (Exception) { Debug.WriteLine("解析返回json失败---\n" + json); return null; } } }
private void SaveAccessToken(AccessTokenInfo AccessToken) { string[] keys = new string[] { "access_token", "expires_time", "refresh_token" }; foreach (string key in keys) { if (!localSetting.Values.ContainsKey(key)) { localSetting.Values.Add(key, ""); }; } localSetting.Values["access_token"] = AccessToken.AccessToken; localSetting.Values["refresh_token"] = AccessToken.RefreshToken; localSetting.Values["expires_time"] = AccessToken.ExpiresTime.ToString(); localSetting.Values["request_time"] = AccessToken.RequestTime.ToString(); }