oAuth2 RestClient 获取令牌不工作,即使 Postman 工作
oAuth2 RestClient Get token not working even though Postman works
以下是在 Postman 中获取成功令牌所需的参数。
代币名称:Sage Access Token
授权类型:授权码
回调URL: https://dummyaddress
使用浏览器授权:未勾选
授权URL: https://dummyaddress
访问令牌URL:https://id.sage.com/oauth/token
客户端 ID:您的客户端 ID
客户端密码:您的客户端密码
范围:openid 配置文件电子邮件 offline_access
客户端身份验证:作为基本身份验证发送 header
当我尝试通过代码时,我得到了 UnAuthorized。
var client = new RestClient("https://id.sage.com/oauth/token");
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
// request.AddParameter("application/x-www-form-urlencoded", "grant_type=Authorization_Code&client_id=DxpXMEWXW1oVjn5l4DwRuw9d0bRzpUlG&client_secret=MlUMsZINFBovHODAjmtfG8rO8kjVyiaDYgvfyeg1lmaMArC2ihyd1jh-5u2GyqU&Scope=openid profile email offline_access&Callback_URL=https://customerdataquestuk--sage--c.visualforce.com/apex/SageCode &Auth_URL=https://id.sage.com/authorize?audience=s200ukipd/sage200", ParameterType.RequestBody);
request.AddParameter("Grant_Type", "Authorization Code");
request.AddParameter("Callback_URL", "https://dummyaddress");
request.AddParameter("Auth_URL", "https://dummyaddress");
request.AddParameter("Client_ID", your client ID);
request.AddParameter("Client_Secret", your client secret);
request.AddParameter("Scope", "openid profile email offline_access");
IRestResponse response = client.Execute(request);
您的代码中缺少基本身份验证 header,您可以使用以下 C# 代码手动创建基本身份验证 header:
var credentials = string.Format("{0}:{1}", clientId, clientSecret);
var headerValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials));
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", headerValue);
或者如果您使用 RestClient
var client = new RestClient("http://localhost");
client.Authenticator = new HttpBasicAuthenticator(clientId,clientSecret);
请查看此文档Client Authentication
以下是在 Postman 中获取成功令牌所需的参数。
代币名称:Sage Access Token
授权类型:授权码
回调URL: https://dummyaddress
使用浏览器授权:未勾选
授权URL: https://dummyaddress
访问令牌URL:https://id.sage.com/oauth/token
客户端 ID:您的客户端 ID
客户端密码:您的客户端密码
范围:openid 配置文件电子邮件 offline_access
客户端身份验证:作为基本身份验证发送 header
当我尝试通过代码时,我得到了 UnAuthorized。
var client = new RestClient("https://id.sage.com/oauth/token");
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
// request.AddParameter("application/x-www-form-urlencoded", "grant_type=Authorization_Code&client_id=DxpXMEWXW1oVjn5l4DwRuw9d0bRzpUlG&client_secret=MlUMsZINFBovHODAjmtfG8rO8kjVyiaDYgvfyeg1lmaMArC2ihyd1jh-5u2GyqU&Scope=openid profile email offline_access&Callback_URL=https://customerdataquestuk--sage--c.visualforce.com/apex/SageCode &Auth_URL=https://id.sage.com/authorize?audience=s200ukipd/sage200", ParameterType.RequestBody);
request.AddParameter("Grant_Type", "Authorization Code");
request.AddParameter("Callback_URL", "https://dummyaddress");
request.AddParameter("Auth_URL", "https://dummyaddress");
request.AddParameter("Client_ID", your client ID);
request.AddParameter("Client_Secret", your client secret);
request.AddParameter("Scope", "openid profile email offline_access");
IRestResponse response = client.Execute(request);
您的代码中缺少基本身份验证 header,您可以使用以下 C# 代码手动创建基本身份验证 header:
var credentials = string.Format("{0}:{1}", clientId, clientSecret);
var headerValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials));
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", headerValue);
或者如果您使用 RestClient
var client = new RestClient("http://localhost");
client.Authenticator = new HttpBasicAuthenticator(clientId,clientSecret);
请查看此文档Client Authentication