【问题标题】:Implementing oauth in SugarCRM using .NET使用 .NET 在 SugarCRM 中实现 oauth
【发布时间】:2012-11-20 18:37:21
【问题描述】:

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

sugarCRM给出的Oauth机制是使用PHPClick Here... 其中,我的应用程序是用 ASP 设计的。

我正在尝试找出解决方案(例如将 php 代码转换为 asp 或在我的应用程序中实现相同的机制)但没有得到解决方案。任何帮助将不胜感激。

【问题讨论】:

  • 尝试使用这个库 dotnetopenauth.net 来复制 OAuth 功能。
  • 之前我尝试使用相同的方法。用于 SugarCRM 的方法与 dotnetopenauth 不同。

标签: php asp.net oauth sugarcrm


【解决方案1】:

我不明白您所说的以 SugarCRM 方式实施的意思。但如果您不能使用 dotnetopenauth,您可以使用 RestSharpHammock

旋转自己的 OAuth

【讨论】:

  • 你看到我在我的问题中提到的链接了吗?用于auth 的方法对于sugarcrm 与传统的方法不同。如果它支持用于正常身份验证的方法,我会使用 dotnetopenauth。
  • 吊床链接已失效。
  • @RohnAdams 谢谢。修复了链接。
【解决方案2】:

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

这就是我所做的......一切都在我的控制台应用程序中。这是一个概念证明,所以现在一切都是硬编码的!

Daniel Crenna 使用 Nuget 安装 OAuth

第 1 步:建立消费者密钥

进入 SugarCRM 上的 Admin -> OAuth Keys 部分并创建一个新记录,我使用了 Key & Secret。

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

这是我花了很长时间才弄清楚的棘手部分,请注意 requesturl 在客户端中没有查询部分,并且您已将其添加到 GetAuthorizationQuery 调用和实际的 WebRequest url。

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

步骤 3 批准请求令牌

访问上面的url“authorize_url”并添加&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"]);
}

Token 和 TokenSecret 来自 Step 2,Verifier 是 Step 3 的 Auth Code。

第 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 步让它做点什么......

交给你了!

相当痛苦的经历,但至少它对我有用.....

提姆

【讨论】:

  • 谢谢 tim,我会试一试,让您知道吗?以及如何将项目中的活动同步到sugarCRM(我的意思是访问方法)。在我的情况下,当我将联系人添加到我的模块中时,我想将它们添加到sugarCRM。
  • Tim,你能帮我解决这个问题,用 Java 实现吗?
  • 抱歉,对Java一无所知。
猜你喜欢
  • 2013-09-25
  • 2021-05-15
  • 2015-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-27
  • 2021-01-12
相关资源
最近更新 更多