Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

How to use RefreshToken to request updated AccessToken

$
0
0

In this article and this one which is linked to from the first, I see references to using the 'refresh token' value to request an updated AccessToken which is used to communicate with SharePoint. Are there any samples of how to use this value to perform the request?

I've found (I think) an example that describes the process but I get a 400 Bad Request when making the request. What I've done is:

  1. Using SharePointContextToken from TokenHelper class which is included in the project template, extract the SecurityTokenServiceUri value to use as the request uri and RefreshToken value is also a property exposed by this token
  2. Create a HttpWebRequest and POST the data as described here at the very end of the article - sample code of the request is below

I've tried using both the encoded and plain data string with the same (400) response.

var request = WebRequest.CreateHttp(token.SecurityTokenServiceUri);
request.Method = "POST";
request.Accept = "application/json;odata=verbose";
request.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0";
var data = "code={2}&client_id={0}&client_secret={1}&grant_type=refresh_token".FormatString(ConfigurationManager.AppSettings["ClientId"], ConfigurationManager.AppSettings["ClientSecret"], token.RefreshToken);
var encodedData = System.Web.HttpUtility.UrlEncode(data);
var stOut = new StreamWriter(request.GetRequestStream());
	stOut.Write(data);
	stOut.Close();
try {
	var response = request.GetResponse();
	var sResponse = string.Empty;
	using (StreamReader stIn = new StreamReader(response.GetResponseStream())) {
		sResponse = stIn.ReadToEnd();
	}
	HttpContext.Current.Response.Write("$$$" + sResponse);
} catch (Exception ex) {
	HttpContext.Current.Response.Write("$$$" + ex.Message);
}




Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>