Unable to get access_token and refresh_token from API

Team,

I’m using below code to call infusionsoft API to get refresh token but getting error “The remote server returned an error: (400) Bad Request.”

Please have a look into my code and provide a solution to get access_token and refresh_token.

Here is my code

public static void Auth()
{
string RefreshToken = “”, DeveloperAppKey= “”, DeveloperAppSecret= “”;
string tokenUrl = “https://api.infusionsoft.com/token?grant_type=refresh_token&refresh_token=” +
RefreshToken;

        HttpWebRequest request = HttpWebRequest.Create(tokenUrl) as HttpWebRequest;
        request.Method = "POST";
        request.KeepAlive = true;
        request.ContentType = "application/x-www-form-urlencoded";
        try
        {
            string authorizationHeaderText =
                Convert.ToBase64String(Encoding.UTF8.GetBytes(DeveloperAppKey + ":" + DeveloperAppSecret));
            request.Headers[HttpRequestHeader.Authorization] = "Basic " + authorizationHeaderText;

            string resultJSON = string.Empty;
            using (WebResponse response = request.GetResponse())
            {
                var sr = new StreamReader(response.GetResponseStream());
                resultJSON = sr.ReadToEnd();
                sr.Close();
            }

            var jsonSerializer = new JavaScriptSerializer();

            //var tokenData = jsonSerializer.Deserialize<TokenData>(resultJSON);

            Console.WriteLine(resultJSON);
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception);
        }
        finally
        {
            Console.ReadLine();
        }
    }

Its urgent , please help.

Thanks

You can’t send the grant_type and refresh_token as query params. The need to be posted in the body.

Made the changes , but still getting bad request.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;

namespace InfusionSoftApp.Models
{
public class ClassToken
{
public static void Auth()
{
string RefreshToken = “", DeveloperAppKey= "", DeveloperAppSecret= "********************************”;
string tokenUrl = “https://api.infusionsoft.com/token”;

        HttpWebRequest request = HttpWebRequest.Create(tokenUrl) as HttpWebRequest;
        request.Method = "POST";
        request.KeepAlive = true;
        request.ContentType = "application/x-www-form-urlencoded";
        //request.ContentLength = 1;
        try
        {
            RefreshToken refmodel = new RefreshToken();
            refmodel.grant_type = "refresh_token";
            refmodel.refresh_token = "*********************";
            string inputJsontag = (new JavaScriptSerializer()).Serialize(refmodel);
            
            string authorizationHeaderText =
                Convert.ToBase64String(Encoding.UTF8.GetBytes(DeveloperAppKey + ":" + DeveloperAppSecret));
            request.Headers[HttpRequestHeader.Authorization] = "Basic " + authorizationHeaderText;

            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                streamWriter.Write(refmodel);
            }

            var httpResponse = (HttpWebResponse)request.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
            }
            var jsonSerializer = new JavaScriptSerializer();
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception);
        }
        finally
        {
            Console.ReadLine();
        }
    }
}

}

Please reply with working code.
Thanks

If you are still getting the same error of missing the content length, then you need to the send that header. Have you looked the docs for HttpWebRequest? HttpWebRequest.ContentLength Property (System.Net) | Microsoft Docs

You are not setting this header which makes it an invalid http request.

public static void Auth()
{
string RefreshToken = “********************”, DeveloperAppKey= “”, DeveloperAppSecret= “”;
string tokenUrl = “https://api.infusionsoft.com/token”;

        HttpWebRequest request = HttpWebRequest.Create(tokenUrl) as HttpWebRequest;
        request.Method = "POST";
        request.KeepAlive = true;
        request.ContentType = "application/x-www-form-urlencoded";
       
        string postData = "grant_type=refresh_token&refresh_token=*****************"; //+ inputData;
        ASCIIEncoding encoding = new ASCIIEncoding();
        byte[] byte1 = encoding.GetBytes(postData);
        request.ContentLength = byte1.Length;
        try
        {
            string authorizationHeaderText =
                Convert.ToBase64String(Encoding.UTF8.GetBytes(DeveloperAppKey + ":" + DeveloperAppSecret));
            request.Headers[HttpRequestHeader.Authorization] = "Basic " + authorizationHeaderText;
            Stream newStream = request.GetRequestStream();

            newStream.Write(byte1, 0, byte1.Length);
            // Close the Stream object.
            newStream.Close();
            WebResponse response = request.GetResponse();
            //var httpResponse = (HttpWebResponse)request.GetResponse();               

        }
        catch (Exception exception)
        {
            Console.WriteLine(exception);
        }
    }
}

with reference to above link, modified the code but i dont understand what i am doing wrong because it still bad request. :slightly_frowning_face:

Are you getting the same error?