2016-10-10 94 views
1

我想在UWP上開發一個小型的twitter應用程序。如何在UWP上實現Twitter的oauth2?

  • 取得OAuth /認證
  • 取得OAuth /授權
  • POST的OAuth/ACCESS_TOKEN
  • POST的OAuth/request_token
  • POST的oauth2/invalidate_token
  • POST的oauth2 /令牌

這是更多的信息。我很困惑。所以我想通過ComsumerKey,ComsumerKeySecret,Callback Uri登錄到Twitter。並獲取access_token從api.twitter.com訪問數據。 我認爲我必須在使用POST oauth2/token之前(是否錯誤?)。

<!-- begin snippet: C# hide: false console: true babel: false --> 

<!-- language: lang-html --> 

public async Task<string> GetAccessTokenBYAuthorizeTwitter() 
     { 
      //throw new NotImplementedException(); 
      var client = new HttpClient(); 
      string postData = "grant_type=client_credentials"; 
      var request = await client.PostAsync(ResourceUrl,new StringContent(postData)); 
      //request headers 
      client.DefaultRequestHeaders.Add("Authorization", string.Format("Basic {0}", Convert.ToBase64String(Encoding.UTF8.GetBytes(ComsumerKeyAndSecret)))); 
      client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); 

      return await request.Content.ReadAsStringAsync(); 
     } 

<!-- end snippet --> 

例如:我嘗試登錄Vimeo併成功。

public async Task<string> AuthorizeWithVimeo() 
    { 
     var clientId = "b8e1bff5d5d1f2c90f61017b135960adb42f5fe2"; 

     var SpotifyUrl = "https://api.vimeo.com/oauth/authorize?client_id=" + Uri.EscapeDataString(clientId) + "&response_type=code&redirect_uri=" + Uri.EscapeDataString("https://example/callback") + "&state=xyzbc"; 
     var StartUri = new Uri(SpotifyUrl); 
     var EndUri = new Uri("https://example/callback"); 

     WebAuthenticationResult WebAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, StartUri, EndUri); 
     if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success) 
     { 
      var responseData = WebAuthenticationResult.ResponseData; 

      //await GetSpotifyUserNameAsync(WebAuthenticationResult.ResponseData.ToString()); 
      return responseData; 
     } 
     else if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.ErrorHttp) 
     { 
      return $"HTTP Error returned by AuthenticateAsync() : {WebAuthenticationResult.ResponseErrorDetail.ToString()}"; 
     } 
     else 
     { 
      return $"Error returned by AuthenticateAsync() : {WebAuthenticationResult.ResponseStatus.ToString()}"; 
     } 
    } 
+0

你訪問哪些API?客戶特定或公共數據有不同的用戶數據流 –

+0

第一,我需要登錄並獲取訪問令牌之前...... –

回答

0

這應該工作

var client = new HttpClient(); 

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
    "Basic", 
    Convert.ToBase64String(
     Encoding.ASCII.GetBytes(
      string.Format("{0}:{1}", Uri.EscapeDataString(oauth_consumer_key), 
          Uri.EscapeDataString(oauth_consumer_secret))))); 

client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip"); 
client.DefaultRequestHeaders.TryAddWithoutValidation("Host", "api.twitter.com"); 

var content = new StringContent(postBody); 
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); 
HttpResponseMessage response =await client.PostAsync(oauth_url, content); 
response.EnsureSuccessStatusCode(); 

using (var responseStream = response.Content.ReadAsStreamAsync()) 
using (var decompressedStream = new GZipStream(responseStream.Result, CompressionMode.Decompress)) 
using (var streamReader = new StreamReader(decompressedStream)) 
{ 
var rawJWt = streamReader.ReadToEnd(); 
var jwt = JsonConvert.DeserializeObject(rawJWt); 
} 

More info

相關問題