Mil*_*war 7 php asp.net oauth sugarcrm
我有一个在.net框架中开发的Web应用程序.我试图在sugarCRM中实现Oauth,以便将它与我的应用程序集成.
SugarCRM给出的Oauth机制是使用PHP Click Here ...其中,我的应用程序是用ASP设计的.
我试图找出解决方案(如将PHP代码转换为asp或在我的应用程序中实现相同的机制)同样但没有解决方案.任何帮助将不胜感激.
经过多次痛苦,我的.Net代码在SugarCRM上运行.....
这就是我所做的....所有在我的控制台应用程序中.这是一个概念验证,因此现在已经硬编码了!
使用Nuget安装OAuth,Daniel Crenna
进入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"]);
}
Run Code Online (Sandbox Code Playgroud)
这是我花了很多时间才弄清楚的棘手部分,注意请求在客户端没有查询部分,并且您已将它添加到GetAuthorizationQuery调用和实际的WebRequest URL.
记下为第4步准备的3个项目.
访问上面的网址"authorize_url",并添加&token ="oauth_token".这是:
http://localhost/index.php?module=OAuthTokens&action=authorize&token=adae15a306b5
Run Code Online (Sandbox Code Playgroud)
授权令牌并记录令牌授权码.
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"]);
}
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
在这里,我使用JSON.Net将Json解析为动态对象,以便轻松访问id.
给你!
非常痛苦的经历,但至少它为我工作.....
蒂姆
| 归档时间: |
|
| 查看次数: |
2541 次 |
| 最近记录: |