1

I am unable to refresh the Access Token once it expires. I'm not sure what's wrong and I followed as per documentation. I am using WCF service and getting the below error.

{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Connection: 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 Cache-Control: no-store Date: Mon, 11 Jun 2018 12:40:21 GMT Set-Cookie: __cfduid=d9ac433a81ce1ec1aedad217862472b131528720820; expires=Tue, 11-Jun-19 12:40:20 GMT; path=/; domain=.lightspeedapp.com; HttpOnly; Secure Server: cloudflare Content-Length: 69 Content-Type: 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;
                        }
                    }
                }
            }
        }
Ivan Smetanin
  • 1,999
  • 2
  • 21
  • 28
A. Gopi
  • 21
  • 3

1 Answers1

1

I got the solution. Below is the changes... additionally we have to get the refresh Token while we getting Access Token at initially.

                    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;
                        }
                    }
A. Gopi
  • 21
  • 3