Created
December 12, 2016 14:33
-
-
Save hatsunea/b01abb3ddc0414157ba8d12bda9311e0 to your computer and use it in GitHub Desktop.
Azure API認証
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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