Skip to content

Instantly share code, notes, and snippets.

@hatsunea
Created December 12, 2016 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hatsunea/b01abb3ddc0414157ba8d12bda9311e0 to your computer and use it in GitHub Desktop.
Save hatsunea/b01abb3ddc0414157ba8d12bda9311e0 to your computer and use it in GitHub Desktop.
Azure API認証
/// <summary>
/// OAuth認証を実行する
/// </summary>
/// <returns></returns>
private async Task<string> GetAzureDataMarketToken()
{
var properties = new Dictionary<string, string>
{
{ "grant_type", "client_credentials" },
{ "client_id", _clientId},
{ "client_secret", _clientSecret },
{ "scope", "http://api.microsofttranslator.com" }
};
using (var req = new HttpClient())
{
var DataMarketAddress = new Uri("https://datamarket.accesscontrol.windows.net/v2/OAuth2-13");
var authentication = new FormUrlEncodedContent(properties);
var res = await req.PostAsync(DataMarketAddress, authentication);
using (Stream stream = await res.Content.ReadAsStreamAsync())
{
var serializer = new DataContractJsonSerializer(typeof(AdmAccessToken));
AdmAccessToken token = (AdmAccessToken)serializer.ReadObject(await res.Content.ReadAsStreamAsync());
return token.access_token;
}
}
}
}
public class AdmAccessToken
{
public string access_token { get; set; }
public string token_type { get; set; }
public string expires_in { get; set; }
public string scope { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment