1

访问令牌过期后我无法刷新。我不确定出了什么问题,我按照文档进行了操作。我正在使用 WCF 服务并收到以下错误。

{StatusCode:400,ReasonPhrase:'Bad Request',版本:1.1,内容:System.Net.Http.StreamContent,标头:{连接:keep-alive
x-frame-options:SAMEORIGIN X-XSS-Protection:1;mode=block
X-Content-Type-Options: nosniff Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" CF -RAY:4294254a0b608a9d-BOM 缓存控制:无存储日期:2018 年 6 月 11 日星期一 12:40:21 GMT 设置 Cookie:__cfduid=d9ac433a81ce1ec1aedad217862472b131528720820;到期=格林威治标准时间 19 年 6 月 11 日星期二 12:40:20;路径=/; 域=.lightspeedapp.com;仅http;安全服务器:cloudflare 内容长度:69 内容类型:application/json }}


public string RefreshToken(string clientsecretkey, string clientkey, string refreshToken)
    {

        string newToken = "";
        int expTime = 0;
        string scope = "";
        string type = "";

        try
        {
            using (var client = new HttpClient())
            {
                using (var content = new MultipartFormDataContent())
                {
                    var values = new[]
                {
                    new KeyValuePair<string, string>("refresh_token", refreshToken),                       
                    new KeyValuePair<string, string>("client_id",clientkey),
                    new KeyValuePair<string, string>("client_secret",clientsecretkey),
                    new KeyValuePair<string, string>("grant_type", "refresh_token")

                };
                    foreach (var keyValuePair in values)
                    {
                        content.Add(new StringContent(keyValuePair.Value), keyValuePair.Key);
                    }                        

                    var fileContent = new ByteArrayContent(new byte[100]);
                    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                    {
                        FileName = "Foo.txt"
                    };
                    content.Add(fileContent);

                    var requestUri = "https://cloud.lightspeedapp.com/oauth/access_token.php";
                    HttpResponseMessage result = client.PostAsync(requestUri, content).Result;

                    if (result.StatusCode == HttpStatusCode.OK)
                    {
                        var smessage = result.Content.ReadAsAsync<UserCredentials>(new[] { new JsonMediaTypeFormatter() }).Result;

                        if (smessage != null)
                        {
                            newToken = smessage.AccessToken;
                            expTime = smessage.ExpiresIn;
                            scope = smessage.Scope;
                            type = smessage.TokenType;
                        }
                    }
                }
            }
        }
4

1 回答 1

1

我得到了解决方案。以下是更改...此外,我们必须在最初获取访问令牌时获取刷新令牌。

                    if (result.StatusCode == HttpStatusCode.OK)
                    {
                        var smessage = result.Content.ReadAsAsync<UserCredentials>(new[] { new JsonMediaTypeFormatter() }).Result;

                        if (smessage != null)
                        {

                            newToken = smessage.AccessToken;
                            expTime = smessage.ExpiresIn;
                            scope = smessage.Scope;
                            type = smessage.TokenType;
                            refToken = smessage.RefreshToken;
                        }
                    }
于 2018-06-12T14:04:36.500 回答