Skip to content

Instantly share code, notes, and snippets.

@jfversluis
Last active August 13, 2016 09:15
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 jfversluis/23e6bfef80e2b93af60be649dcc1bb91 to your computer and use it in GitHub Desktop.
Save jfversluis/23e6bfef80e2b93af60be649dcc1bb91 to your computer and use it in GitHub Desktop.
Bot Framework code to get bytes from Attachment on Skype
byte[] imageBytes = null;
// For Skype we need to get an OAuth token to access the content url
if (activity.ChannelId.ToLower() == "skype")
{
using (var httpClient = new HttpClient())
{
// Request OAuth token
var formValues = new KeyValuePair<string, string>[]
{
new KeyValuePair<string, string>("client_id", ConfigurationManager.AppSettings["MicrosoftAppId"]),
new KeyValuePair<string, string>("client_secret", ConfigurationManager.AppSettings["MicrosoftAppPassword"]),
new KeyValuePair<string, string>("grant_type", "client_credentials"),
new KeyValuePair<string, string>("scope", "https://graph.microsoft.com/.default")
};
// Anonymous definition for return object
var definition = new { access_token = "" };
var tokenJson = await httpClient.PostAsync("https://login.microsoftonline.com/common/oauth2/v2.0/token",
new FormUrlEncodedContent(formValues));
var token = JsonConvert.DeserializeAnonymousType(await tokenJson.Content.ReadAsStringAsync(), definition);
// Set Bearer: <token> header for the request
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.access_token);
imageBytes = await httpClient.GetByteArrayAsync(attachment.ContentUrl);
}
}
else
imageBytes = BotHelper.ConvertContentToByteArray(attachment.ContentUrl);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment