Implementing oauth in SugarCRM using .NET

I have a web application developed in the .NET Framework. I am trying to implement Oauth in sugarCRM to integrate it with my applications.

SugarCRM's Oauth engine uses PHP. Click here ... where as, my application is developed in ASP.

I am trying to find a solution (for example, convert php code to asp or implement the same mechanism in my application), but have not received any solution. Any help would be appreciated.

+7
source share
2 answers

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

+7
source

I did not understand what you had in mind when using SugarCRM. But if you cannot use dotnetopenauth , you can rotate your own OAuth using RestSharp or Hammock

+2
source

All Articles