我有一个在 .net 框架中开发的 Web 应用程序。我正在尝试在 SugarCRM 中实现 Oauth,以便将其与我的应用程序集成。
SugarCRM 提供的 Oauth 机制是使用 PHP Click Here ... 其中,我的应用程序是用 ASP 设计的。
我正在尝试找出解决方案(例如将 php 代码转换为 asp 或在我的应用程序中实现相同的机制)但没有得到解决方案。任何帮助将不胜感激。
我有一个在 .net 框架中开发的 Web 应用程序。我正在尝试在 SugarCRM 中实现 Oauth,以便将其与我的应用程序集成。
SugarCRM 提供的 Oauth 机制是使用 PHP Click Here ... 其中,我的应用程序是用 ASP 设计的。
我正在尝试找出解决方案(例如将 php 代码转换为 asp 或在我的应用程序中实现相同的机制)但没有得到解决方案。任何帮助将不胜感激。
在经历了很多痛苦之后,我的 .Net 代码在 SugarCRM 上工作......
这就是我所做的......一切都在我的控制台应用程序中。这是一个概念证明,所以现在一切都是硬编码的!
Daniel Crenna 使用 Nuget 安装 OAuth
进入 SugarCRM 上的 Admin -> OAuth Keys 部分并创建一个新记录,我使用了 Key & Secret。
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 个项目。
访问上面的 url "authorize_url" 并添加 &token="oauth_token"。为此:
http://localhost/index.php?module=OAuthTokens&action=authorize&token=adae15a306b5
授权令牌并记录令牌授权码。
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 来自第 2 步,Verifier 是第 3 步中的 Auth Code。
我只是使用文档推荐的会话 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。
交给你了!
相当痛苦的经历,但至少它对我有用......
蒂姆