2017-08-31 47 views
0

訪問令牌我使用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(); 

任何幫助,將不勝感激

回答

0

試試這個

public async Task<string> GetAccessToken(string clientId, string redirectUri,string clientSecret, string authCode, string grantType) 
     { 

      var testData = new Data(){ClientId = clientId, 
       ClientSecret = clientSecret,    
      RedirectUri = redirectUri, 
      GrantType = grantType, 
      Code = authCode 
      }; 

      var uri = 
       $"https://www.universe.com/oauth/token"; 
      System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 

      using (var httpClient = new HttpClient()) 
      { 
       httpClient.DefaultRequestHeaders.Add("Accept", "application/json"); 
       var str = JsonConvert.SerializeObject(testData); 
       var dataContent = new StringContent(str); 
       var response = await httpClient.PostAsync(uri, dataContent); 
       return await response.Content.ReadAsStringAsync(); 
      } 

     } 
     public class Data 
     { 
      [JsonProperty("code")] 
      public string Code { get; set; } 
      [JsonProperty("grant_type")] 
      public string GrantType { get; set; } 
      [JsonProperty("client_id")] 
      public string ClientId { get; set; } 
      [JsonProperty("client_secret")] 
      public string ClientSecret { get; set; } 
      [JsonProperty("redirect_uri")] 
      public string RedirectUri { get; set; } 
     } 
+0

謝謝。我如何將它集成到我上面提到的現有代碼中? – TadeKite

+0

首先,請告訴我,爲什麼當你的請求是GET時使用POST?我想如果你將GET取代POST,它可能會幫助你。如果我不會使用httpclient – Giltanas

+0

編寫代碼如果你看文檔訪問令牌在他們的指南上使用POST https://developers.universe.com/v2/reference – TadeKite

0

,我認爲你應該通過在體內的請求參數(不將它們嵌入在URL)

public static string GetAccessToken(string clientId, string secret, string AUTHORIZATION_CODE, string redirect_uri) 
{ 
    try 
    { 
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://www.universe.com/oauth/token"); 
    webRequest.Method = "POST"; 
    webRequest.Accept = "application/json"; 
    webRequest.ContentType = "application/x-www-form-urlencoded"; 
    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 
    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(string.Format("grant_type=authorization_code&client_id={0}&client_secret={1}&code={2}&redirect_uri={3}", clientId, secret, AUTHORIZATION_CODE, redirect_uri)); 
    webRequest.ContentLength = buffer.Length; 
    using (var reqStrm = webRequest.GetRequestStream()) 
    { 
    reqStrm.Write(buffer, 0, buffer.Length); 
    } 

    using (HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse()) 
    { 
     using (StreamReader reader = new StreamReader(resp.GetResponseStream())) 
     { 
     return reader.ReadToEnd(); 
     } 
    } 

    } 
    catch (Exception ex){return ex.Message;} 
} 

也許你仍然會得到錯誤,因爲文檔here

redirect_uri:REDIRECT_URI - URL必須完全匹配傳遞給/ authorize的redirect_uri,這意味着您應該先調用/ authorize。這兩個請求也應該使用相同的CookieContainer。 我還建議您將UserAgent標頭添加到您的請求