Now i want to refresh an access token using the REST api.
Here the Auth header is “Basic + base64_encode(CLIENT_ID + ‘:’ + CLIENT_SECRET)” where CLIENT_ID & CLIENT_SECRET are taken from the developer dashboard, shown in image 1. This tool was used to encode the id and secret - https://www.base64encode.org before entering it into curl
will not resolve correctly. Your header should be "Authorization: Basic "+Base64(id:secret) but the way your quotes (double and single) and plus signs are arranged, that’s not what you’ll get. Have you tried printing this to the screen to see that it resolves to the string you’re looking for?
{“error”:“invalid_request”,“error_description”:“response_type or grant_type is required”}
As for the plus signs… that is cut and pasted from the documentaiton.
Example pseudo code: Basic + base64_encode(CLIENT_ID + ‘:’ + CLIENT_SECRET) here OAuth2 Authentication - Keap Developer Portal
@Gavin_Nathan Thanks for taking the time to post what you’ve tried. I think at least part of the trouble is how curl is being told to make the request.
Instead, of using -d (or --data) with JSON, use query string parameters. Let me know how that goes for you.
(Updated this post to remove erroneous info and provide correct info as provided by Brad.)
public OAuthModel RefreshApiToken(string refreshToken)
{
// exchange authorization code at authorization server for an access and refresh token
Dictionary<string, string> post = null;
post = new Dictionary<string, string>
{
{"refresh_token", refreshToken},
{"grant_type", "refresh_token"}
};
using (var client = new HttpClient())
{
try
{
client.BaseAddress = new Uri(tokenUrl);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", _businessId, _apiKey))));
var postContent = new FormUrlEncodedContent(post);
var tokenResponse = client.PostAsync("token", postContent).Result;
tokenResponse.EnsureSuccessStatusCode(); // Throw in not success
var content = tokenResponse.Content.ReadAsStringAsync().Result;
var responseModel = JsonConvert.DeserializeObject<OAuthModel>(content);
return responseModel;
}
catch (HttpRequestException e)
{
}
return new OAuthModel();
}
}