Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Refresh the current Salesforce access token
- /// </summary>
- /// <param name="token">
- /// The refresh token provided by Salesforce's OAuth Server on initial login
- /// </param>
- /// <returns>
- /// Access token, endpoint URL and other nice things.
- /// </returns>
- /// <exception cref="ConnectionException"></exception>
- /// <exception cref="ArguementException"></exception>
- public static SessionInfo RefreshToken(string clientId, string token)
- {
- if (string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(token)) { throw new ArgumentException("clientId and token are required"); }
- string postData = string.Format("grant_type=refresh_token&client_id={0}&refresh_token={1}", clientId, token);
- byte[] data = Encoding.UTF8.GetBytes(postData);
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Config.RefreshTokenUri);
- request.Method = "POST";
- request.ContentType = "application/x-www-form-urlencoded";
- request.Accept = "application/json";
- request.ContentLength = data.Length;
- using (Stream requestStream = request.GetRequestStream())
- {
- requestStream.Write(data, 0, data.Length);
- }
- WebResponse response = null;
- SessionInfo authResponse = null;
- try
- {
- response = request.GetResponse();
- string authResponseJson = new StreamReader(response.GetResponseStream()).ReadToEnd();
- authResponse = JsonConvert.DeserializeObject<SessionInfo>(authResponseJson);
- }
- catch (WebException ex)
- {
- string errorJson = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
- ex.Response.GetResponseStream().Close();
- ex.Response.Close();
- SessionError error = JsonConvert.DeserializeObject<SessionError>(errorJson);
- throw new ConnectionException(error);
- }
- catch (JsonSerializationException ex)
- {
- System.Diagnostics.Trace.Assert(false, ex.Message); // Not too interested in this
- }
- catch (JsonReaderException ex)
- {
- System.Diagnostics.Trace.Assert(false, ex.Message); // Ditto
- }
- finally
- {
- response.GetResponseStream().Close();
- response.Close();
- }
- return authResponse;
- }
Advertisement
Add Comment
Please, Sign In to add comment