[英]Unable to get Access token and Refresh token
All ClientID, ClientSecret & RedirectURI placed in the web.config
.所有 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>
Now from here I got the authentication code.现在从这里我得到了验证码。
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));
}
Then I got my authorization code:然后我得到了我的授权码:
3Q8tvb9d0fj232NZZIAIaItUIqtAd7
(I store this code in label ie lblcode.Text
) (我将此代码存储在标签中,即
lblcode.Text
)
Then for Access_token & Refresh token, I use this code:然后对于 Access_token 和 Refresh 令牌,我使用以下代码:
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;
}
}
But I'm getting an error:但我收到一个错误:
The request was aborted: Could not create SSL/TLS secure channel.
请求被中止:无法创建 SSL/TLS 安全通道。
Note :注意:
Put this code above把这段代码放在上面
ServicePointManager.Expect100Continue = true;
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
the following code.以下代码。
var request = WebRequest.Create("https://drchrono.com/o/token/");'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.