訪問令牌我使用universe.com API https://developers.universe.com我使用CSHARP擺脫Autorization代碼
當我運行下面的代碼我得到401,未經授權的訪問。
public static string GetAccessToken(string clientId, string secret, string AUTHORIZATION_CODE, string redirect_uri)
{
var uri = string.Format("https://www.universe.com/oauth/token?grant_type=authorization_code&client_id={0}&client_secret={1}&code={2}&redirect_uri={3}", clientId, secret, AUTHORIZATION_CODE, redirect_uri);
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
webRequest.Method = "POST";
webRequest.Accept = "application/json";
webRequest.ContentType = "application/x-www-form-urlencoded";
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var httpResponse = (HttpWebResponse)webRequest.GetResponse();
using (var reader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = reader.ReadToEnd();
//var credentials = JsonConvert.DeserializeObject<Credentials>(result);
//return credentials.accessToken;
return result.ToString();
}
}
這裏是我的測試應用程序的詳細信息:
應用程序ID: 4eae99d7317bf63ba9204accbb76635d528b6a12559464ed1789ef3e5e6ca2f2 揭祕: 04cecca5a04d31e17cd29979de2da585bfd7ce2e2036c41f276131dfbd2d2ef2 Autorization代碼:2477b8473fa06b4816fd33e91d782d2e388642539a4e31ce9560b65d28e7fcff 重定向URI(本地主機):金塔:IETF:WG:的OAuth:2.0 :oob
string clientId =「4eae99d7317bf63ba9204accbb7 6635d528b6a12559464ed1789ef3e5e6ca2f2" ; string secret =「04cecca5a04d31e17cd29979de2da585bfd7ce2e2036c41f276131dfbd2d2ef2」; string AUTHORIZATION_CODE =「2477b8473fa06b4816fd33e91d782d2e388642539a4e31ce9560b65d28e7fcff」; string redirect_uri =「urn:ietf:wg:oauth:2.0:oob」; string token = GetAccessToken(clientId,secret,AUTHORIZATION_CODE,redirect_uri);
Console.WriteLine(token.ToString());
Console.ReadLine();
任何幫助,將不勝感激
謝謝。我如何將它集成到我上面提到的現有代碼中? – TadeKite
首先,請告訴我,爲什麼當你的請求是GET時使用POST?我想如果你將GET取代POST,它可能會幫助你。如果我不會使用httpclient – Giltanas
編寫代碼如果你看文檔訪問令牌在他們的指南上使用POST https://developers.universe.com/v2/reference – TadeKite