使用.NET在SugarCRM中实现oauth

时间:2012-12-03 09:58:51

标签: php asp.net oauth sugarcrm

我有一个在.net框架中开发的Web应用程序。我正在尝试在sugarCRM中实现Oauth,以便将其与我的应用程序集成。

sugarCRM给出的Oauth机制正在使用PHP Click Here ...... 在哪里,我的应用程序是在ASP中设计的。

我试图找出解决方案(比如将PHP代码转换为asp或在我的应用程序中实现相同的机制),但是没有得到解决方案。非常感谢。

2 个答案:

答案 0 :(得分:7)

经过多次痛苦之后,我的.Net代码在SugarCRM上运行了.....

这就是我所做的......所有这些都在我的控制台应用程序中。这是一个概念验证,因此现在已经硬编码了!

使用Nuget来安装Daniel Crenna的OAuth

第1步:建立消费者密钥

进入管理员 - >关于SugarCRM的OAuth Keys部分并创建了一条新记录,我使用了Key&秘密。

第2步:创建请求令牌

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"]);
}

这是我花了很多时间才弄明白这个棘手的部分,请注意请求文件在客户端没有查询部分,并且您已将其添加到GetAuthorizationQuery调用和实际的WebRequest URL中。

记下为步骤4准备的3个项目。

步骤3批准请求令牌

访问上面的网址“authorize_url”,并添加&amp; token =“oauth_token”。因为这是:

http://localhost/index.php?module=OAuthTokens&action=authorize&token=adae15a306b5

授权令牌并记录令牌授权码。

步骤4请求访问令牌

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"]);
}

令牌和TokenSecret来自第2步,验证者是步骤3中的验证码。

步骤5使用访问令牌

我只是使用文档推荐的会话ID,所以要获取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);
}

这里我使用JSON.Net将Json解析为动态对象,以便于访问id。

第6步让它做点什么....

告诉你!

非常痛苦的经历,但至少它为我工作.....

答案 1 :(得分:2)

我没有通过SugarCRM方式实现你的意思。但是,如果您无法使用 dotnetopenauth ,则可以使用RestSharpHammock

旋转自己的OAuth