我正在用C#编写一个控制台应用程序,该应用程序应连接到使用OAuth 2.0的网站。
我正在使用以下代码:
var client = new RestClient(new Uri(BaseUri, "api/oauth2/Token"));
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddHeader("authorization", "Bearer " + user.RefreshToken);
request.AddParameter("application/x-www-form-urlencoded", "grant_type=client_credentials&client_id=" + MyClientIdentifier() + "&client_secret=" + MyClientSecret(), ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
运行代码时,我不断获取状态代码
未经授权
我在做什么错了?
答案 0 :(得分:1)
我相信您为刷新令牌here is the relevant specification使用了错误的grant_type
,因此,如果您的参数有效,则以下代码应该有效。
var client = new RestClient(new Uri(BaseUri, "api/oauth2/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=refresh_token" +
$"&refresh_token={user.RefreshToken}" +
$"&client_id={MyClientIdentifier()}" +
$"&client_secret={MyClientSecret()}",
ParameterType.RequestBody);
IRestResponse response = client.Execute(request);