Hi Shahzad,
The below step you need to follow:
1)Generate Code, providing below details, paste the URL in browser, it will be manual process for the first time
login.xero.com/.../authorize openid profile email accounting.transactions accounting.contacts accounting.contacts.read accounting.settings&state=1264
This will give code as query string, store it in your system.
2)Generate Token
Call the below API:
identity.xero.com/.../token
In your code, you are missing below components:
So your code should look like:
private static async Task<string> GetAccessToken()
{
string accessToken = null;
string refresh_token = null;
string baseUrl = "identity.xero.com/.../token
string clientId = "client_id";
string clientSecret = "client_secrete";
var plainText =ClientID:ClientSecret;
var encodedText = Base64Encode(plainText);
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(baseUrl);
// We want the response to be JSON.
client.DefaultRequestHeaders.Accept.Clear();
string _ContentType = "application/x-www-form-urlencoded";
client .DefaultRequestHeaders.Add("authorization", "Basic " + encodedText);
client .DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(_ContentType));
// Build up the data to POST.
List<KeyValuePair<string, string>> postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("grant_type", "authorization_code"));
postData.Add(new KeyValuePair<string, string>("code", "Code that you get at step 1"));
postData.Add(new KeyValuePair<string, string>("redirect_uri", "redirect_uri"));
var req = new HttpRequestMessage(HttpMethod.Post, "">identity.xero.com/.../token") { Content = new FormUrlEncodedContent(postData) };
var response = cl.SendAsync(req).Result;
var stream = response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
var customerJsonString = response.Content.ReadAsStringAsync();
object responseData = JsonConvert.DeserializeObject(jsonString);
// return the Access Token.
accessToken = ((dynamic)responseData).access_token;
refresh_token = ((dynamic)responseData).refresh_token;
}
return accessToken;
}
Keep, the refresh token saved, and next time you need to the same API and change the header
postData.Add(new KeyValuePair<string, string>("grant_type", "refresh_token")
postData.Add(new KeyValuePair<string, string>("refresh_token", "refreshtoken"));
Hope this helps you.
Regards
Hemant