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()}";
}
}
你訪問哪些API?客戶特定或公共數據有不同的用戶數據流 –
第一,我需要登錄並獲取訪問令牌之前...... –