Jump to content

C# Request BearerToken for the REST API


 Share

Recommended Posts

Hi,

 

Am trying request the token and authorization without going through the browser using C# console application. Its appreciated if any who successfully using same could share the code.

 

    string codeAr = "";
    HttpClientHandler clientHandler = new HttpClientHandler();
    clientHandler.Proxy = WebRequest.GetSystemWebProxy();
    clientHandler.Proxy.Credentials = CredentialCache.DefaultCredentials; 
    clientHandler.UseProxy = true;
    httpClient = new HttpClient(clientHandler);
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    HttpContent requestContent = new FormUrlEncodedContent(new Dictionary<string, string>
{
        {"grant_type","authorization_code"},
        {"code",codeAr},
        {"redirect_uri", "https://oauth.pstmn.io/v1/browser-callback/"},
        {"client_id",clientID},
        {"client_secret",secretKey},
        {"content_type","application/json"},
        {"resource","https://api.intacct.com/ia/api/"}                    
}
    );
    HttpResponseMessage response = httpClient.PostAsync(tokenURL, requestContent).Result;
    String responseString = response.Content.ReadAsStringAsync().Result;
    using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(responseString)))
    {
        // Deserialization from JSON  
        DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(BearerToken));
        BearerToken token = (BearerToken)deserializer.ReadObject(ms);
        oauthToken = token.access_token;
    }

 

 

Thanks in advance

 

with regards

alby

Link to comment
Share on other sites

  • 2 months later...
 Share

×
×
  • Create New...