Skip to content

Instantly share code, notes, and snippets.

@dberke711
Created May 23, 2014 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dberke711/c8d0c9e04c7e76220d4e to your computer and use it in GitHub Desktop.
Save dberke711/c8d0c9e04c7e76220d4e to your computer and use it in GitHub Desktop.
C# sample for getting a Concur OAuth token via native flow
/// <summary>
/// Get a Concur OAuth token using the "Native Flow"
/// </summary>
/// <param name="loginID">Concur login ID of the user that is requesting the token</param>
/// <param name="password">Password for the specified user</param>
/// <param name="appKey">Application authorization key identifying the application that the token will access
/// (the "Key" field from the "New/Modify Partner Application" dialog box on the Web Services administration page.)</param>
/// <returns>Access token, good for one year.</returns>
public string GetOAuthToken(string loginID, string password, string appKey)
{
// Base-64 encode the credentials for basic auth
string encodedCredentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(loginID + ":" + password));
// Create the request and add the credentials and app key to the HTTP headers
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.concursolutions.com/net2/oauth2/accesstoken.ashx");
req.Method = "GET";
req.Headers.Add("Authorization", "Basic " + encodedCredentials);
req.Headers.Add("X-ConsumerKey", appKey);
// Make the request and get the response
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
// Load the XML response and parse the token
XmlDocument doc = new XmlDocument();
doc.Load(resp.GetResponseStream());
string token = doc.SelectSingleNode("/Access_Token/Token").InnerText;
return(token);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment