after a lot of pain, I have my .Net code working on SugarCRM .....
This is what I did ... everything in the console app for me. This is a proof of concept, and therefore everything that is currently hardcoded!
Use Nuget to Install OAuth by Daniel Crenna
Step 1: Install the Consumer Key
Go to Admin → OAuth Keys section on SugarCRM and create a new entry, I used Key and Secret.
Step 2: Creating a Request Token
private static void CreateRequestToken() { // Creating a new instance directly OAuthRequest client = new OAuthRequest { Method = "GET", Type = OAuthRequestType.RequestToken, SignatureMethod = OAuthSignatureMethod.HmacSha1, ConsumerKey = "Key", ConsumerSecret = "Secret", RequestUrl = "http://localhost/service/v4/rest.php", Version = "1.0", SignatureTreatment = OAuthSignatureTreatment.Escaped }; // Using URL query authorization string auth = client.GetAuthorizationQuery(new Dictionary<string, string>() { { "method", "oauth_request_token" } }); var request = (HttpWebRequest)WebRequest.Create("http://localhost/service/v4/rest.php?method=oauth_request_token&" + auth); var response = (HttpWebResponse)request.GetResponse(); NameValueCollection query; using (StreamReader sr = new StreamReader(response.GetResponseStream())) { string result = sr.ReadToEnd(); query = HttpUtility.ParseQueryString(result); } Console.WriteLine(query["authorize_url"]); Console.WriteLine(query["oauth_token"]); Console.WriteLine(query["oauth_token_secret"]); }
This is the tricky part that took a while to figure out, note that requesturl does not contain the request part in the client, and you added it to the GetAuthorizationQuery AND call to the actual WebRequest URL.
Write down the three elements ready for step 4.
Step 3 Approve Request Token
Visit the url "authorize_url" above, and also add & token = "oauth_token". For this was:
http://localhost/index.php?module=OAuthTokens&action=authorize&token=adae15a306b5
Authorize the token and write the Token authorization code.
Step 4 Request an access token
private static void RequestAccessToken() { OAuthRequest client = new OAuthRequest { Method = "GET", Type = OAuthRequestType.AccessToken, SignatureMethod = OAuthSignatureMethod.HmacSha1, ConsumerKey = "Key", ConsumerSecret = "Secret", RequestUrl = "http://localhost/service/v4/rest.php", Version = "1.0", SignatureTreatment = OAuthSignatureTreatment.Escaped, Token = "adae15a306b5", TokenSecret = "e1f47d2a9e72", Verifier = "33e2e437b2b3" }; // Using URL query authorization string auth = client.GetAuthorizationQuery(new Dictionary<string, string>() { { "method", "oauth_access_token" } }); var request = (HttpWebRequest)WebRequest.Create("http://localhost/service/v4/rest.php?method=oauth_access_token&" + auth); var response = (HttpWebResponse)request.GetResponse(); NameValueCollection query; using (StreamReader sr = new StreamReader(response.GetResponseStream())) { string result = sr.ReadToEnd(); query = HttpUtility.ParseQueryString(result); } Console.WriteLine(query["oauth_token"]); Console.WriteLine(query["oauth_token_secret"]); }
Token and TokenSecret are shown in step 2, Verifier is the Auth code from step 3.
Step 5 Use the Access Token
I just use the session id as recommended in the documentation, so to get sessionId
private static void GetSessionId() { OAuthRequest client = new OAuthRequest { Method = "GET", Type = OAuthRequestType.ProtectedResource, SignatureMethod = OAuthSignatureMethod.HmacSha1, ConsumerKey = "Key", ConsumerSecret = "Secret", RequestUrl = "http://localhost/service/v4/rest.php", Version = "1.0", SignatureTreatment = OAuthSignatureTreatment.Escaped, Token = "adae15a306b5", TokenSecret = "2d68ecf5152f" }; string auth = client.GetAuthorizationQuery(new Dictionary<string, string>() { { "method", "oauth_access" }, { "input_type", "JSON" }, { "request_type", "JSON" }, { "response_type", "JSON" } }); var request = (HttpWebRequest)WebRequest.Create("http://localhost/service/v4/rest.php?method=oauth_access&input_type=JSON&request_type=JSON&response_type=JSON&" + auth); var response = (HttpWebResponse)request.GetResponse(); dynamic o; using (StreamReader sr = new StreamReader(response.GetResponseStream())) { string result = sr.ReadToEnd(); o = Newtonsoft.Json.JsonConvert.DeserializeObject(result); } Console.WriteLine("SessionId: {0}", o.id); }
Here I use JSON.Net to parse Json in a dynamic object for easy access to id.
Step 6 Do Something ...
For you!
Pretty painful experience, but at least its work for me .....
Tim