所有的 ClientID, ClientSecret & RedirectURI 都放在 web.config
中。
<appSettings>
<add key="redirectURI" value="http://localhost:55593/oauthplayground" />
<add key="clientId" value="uX4YpHHNm****ltekoG" />
<add key="clientSecret" value="K5cSv3izT1GZ9PXnaWWfRWbTv10*****O3JkYFMlWMF3FhBtjyk0FqJduGJZSAL7B1DngJyxgX3KKNSD0Bqdv" />
</appSettings>
现在我从这里得到了验证码。
static string redirectURI = ConfigurationManager.AppSettings["redirectURI"];
static string clientID = ConfigurationManager.AppSettings["clientID"];
static string clientSecret = ConfigurationManager.AppSettings["clientSecret"];
protected void btnclick_Click(object sender, EventArgs e)
{
Response.Redirect(String.Format("https://d****o.com/o/authorize/?response_type=code&client_id={0}&redirect_uri={1}", clientID, redirectURI));
}
然后我得到了我的授权码:
3Q8tvb9d0fj232NZZIAIaItUIqtAd7
(我将此代码存储在标签中,即 lblcode.Text
)
然后对于 Access_token 和刷新 token ,我使用此代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString.Get("code") != null)
{ string AccessToken = string.Empty;
lblcode.Text = Request.QueryString["code"].ToString();
string RefreshToken = ExchangeAuthorizationCode(lblcode.Text, out AccessToken);
}
}
}
private string ExchangeAuthorizationCode(string code, out string accessToken)
{
accessToken = string.Empty;
string ClientSecret = clientSecret;
string ClientId = clientID;
//get this value by opening your web app in browser.
string RedirectUrl = redirectURI;
var Content = "code=" + code + "&client_id=" + ClientId + "&client_secret=" + ClientSecret + "&redirect_uri=" + RedirectUrl + "&grant_type=authorization_code";
var request = WebRequest.Create("https://drchrono.com/o/token/");
request.Method = "POST";
request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequired;
byte[] byteArray = Encoding.UTF8.GetBytes(Content);
request.ContentType = "application/x-www-urlencoded";
request.ContentLength = byteArray.Length;
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
//System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
//ServicePointManager.ServerCertificateValidationCallback = (snder, cert, chain, error) => true;
using (Stream dataStream = request.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
}
var Response = (HttpWebResponse)request.GetResponse();
Stream responseDataStream = Response.GetResponseStream();
StreamReader reader = new StreamReader(responseDataStream);
string ResponseData = reader.ReadToEnd();
reader.Close();
responseDataStream.Close();
Response.Close();
if (Response.StatusCode == HttpStatusCode.OK)
{
var ReturnedToken = JsonConvert.DeserializeObject<Token>(ResponseData);
if (ReturnedToken.refresh_token != null)
{
accessToken = ReturnedToken.access_token;
return ReturnedToken.refresh_token;
}
else
{
return null;
}
}
else
{
return string.Empty;
}
}
但是我得到一个错误:
The request was aborted: Could not create SSL/TLS secure channel.
注意:
- 使用 POSTMAN,我获得了所有数据。这意味着所有 API 都在工作 正确。
- 在代码中列出项目注释,我使用了那些但同样的问题 发生。
- 我还用 IISCrypto.exe 检查了我的系统,我在其中 可以看到我所有的服务器,客户端协议(protocol)(SSL 2.0,SSL 3.0,TLS 1.0, TLS 1.1、TLS 1.2) 已启用。
最佳答案
把这段代码放在上面
ServicePointManager.Expect100Continue = true;
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
下面的代码。
var request = WebRequest.Create("https://drchrono.com/o/token/");'
关于c# - 无法获取访问 token 和刷新 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59188499/