function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NMackeyNMackey 

OAuth 2.0 and the REST API

I'm having a look at REST and logging in via OAuth 2.0 client side flow and running into problems making a web request once I'm authenticated.

 

I don't think it's the actual auth because it's working fine on an iPhone application (Using traditional SOAP API) and the refresh and access tokens I'm getting match when I do from that app if I use the same client key.

 

My code is this:

 

 

void LoggedIn(object o, LoginEventArgs e)
{
    System.Diagnostics.Trace.WriteLine(e);

    string url = e.InstanceUrl + "/services/data/v20.0/sobjects/";

    WebRequest request = WebRequest.Create(url);
    request.Headers.Add("Authorization:OAuth " + e.AccessToken);
           
    WebResponse response = request.GetResponse();
    // 403 Error!
}

 

Am I missing something? 

 

Update:

 

I used the returned token via cUrl and got the following response:

 

 

[ {
  "message" : "Session expired or invalid",
  "errorCode" : "INVALID_SESSION_ID"
} 

 

This is weird, should I be setting up a session cookie instead of adding an Authorization header?

 

Best Answer chosen by Admin (Salesforce Developers) 
NMackeyNMackey

A quick update again, I am in error here due to my inexperience with .Net! If you get a 401 or 403 error, a web request will throw an exception and that exception contains a response. I took a look at the content of the response and found this:

 

 

A first chance exception of type 'System.Net.WebException' occurred in System.dll
[{"message":"The REST API is currently in pilot, and is not enabled for your organization.","errorCode":"API_DISABLED_FOR_ORG"}]

 

 

 

string url = e.InstanceUrl + @"/services/data/v20.0/sobjects/";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("Authorization: OAuth " + e.AccessToken);
request.ContentType = "application/json";

try
{
    WebResponse response = request.GetResponse();
}
catch (WebException ex)
{
    System.Diagnostics.Trace.WriteLine(new  StreamReader(ex.Response.GetResponseStream()).ReadToEnd());
}