将访问令牌传递为URL查询参数?

时间:2019-06-14 13:30:12

标签: c# botframework microsoft-graph

我正在创建一个机器人,我必须在其中发送带有从Microsoft Graph获取的图像的HeroCard。问题是我无法通过这种方式发送访问令牌来获取图像。

我在CardImage的构造函数中传递图像URL。没有其他构造函数或函数可以对其进行修改,因此我无法像通常那样获得带有令牌的图像,然后将其发送到CardImage。

是否可以将令牌作为url查询字符串传递?我知道不推荐这样做,所以还有另一种方法可以做到这一点吗? 谢谢您的帮助

1 个答案:

答案 0 :(得分:0)

除非您以某种方式拦截客户端中的http请求并添加Authorization: Bearer <token>头,否则我不认为有办法。

但是,有MSGraph Sample种用途的解决方法。

  1. Prompt the user to log in

  2. Get the photo, using the tokenkeep the base64 string of the photo

  3. 将base64字符串添加到CardImage.Url属性中。

    • 它接受base64字符串,只要其格式为:"data:image/png;base64,<base64String>"
    • This answer可能会帮助您完成这一部分

对于后代,每一步的相关代码

步骤1

在对话框构造函数中:

AddDialog(new OAuthPrompt(
    nameof(OAuthPrompt),
    new OAuthPromptSettings
    {
        ConnectionName = ConnectionName,
        Text = "Please login",
        Title = "Login",
        Timeout = 300000, // User has 5 minutes to login
    }));

第一步:

private async Task<DialogTurnResult> PromptStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
    return await stepContext.BeginDialogAsync(nameof(OAuthPrompt), null, cancellationToken);
}

下一步:

private async Task<DialogTurnResult> LoginStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
    // Get the token from the previous step. Note that we could also have gotten the
    // token directly from the prompt itself. There is an example of this in the next method.
    var tokenResponse = (TokenResponse)stepContext.Result;
    if (tokenResponse != null)
    {
        await stepContext.Context.SendActivityAsync(MessageFactory.Text("You are now logged in."), cancellationToken);
        return await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = MessageFactory.Text("Would you like to do? (type 'me', 'send <EMAIL>' or 'recent')") }, cancellationToken);
    }

    await stepContext.Context.SendActivityAsync(MessageFactory.Text("Login was not successful please try again."), cancellationToken);
    return await stepContext.EndDialogAsync();
}

现在我们有了令牌。

步骤2

// Gets the user's photo
public async Task<PhotoResponse> GetPhotoAsync()
{
    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + _token);
    client.DefaultRequestHeaders.Add("Accept", "application/json");

    using (var response = await client.GetAsync("https://graph.microsoft.com/v1.0/me/photo/$value"))
    {
        if (!response.IsSuccessStatusCode)
        {
            throw new HttpRequestException($"Graph returned an invalid success code: {response.StatusCode}");
        }

        var stream = await response.Content.ReadAsStreamAsync();
        var bytes = new byte[stream.Length];
        stream.Read(bytes, 0, (int)stream.Length);

        var photoResponse = new PhotoResponse
        {
            Bytes = bytes,
            ContentType = response.Content.Headers.ContentType?.ToString(),
        };

        if (photoResponse != null)
        {
            photoResponse.Base64String = $"data:{photoResponse.ContentType};base64," +
                                            Convert.ToBase64String(photoResponse.Bytes);
        }

        return photoResponse;
    }
}

步骤3

public static string ImageToBase64()
{
    var path = System.Web.HttpContext.Current.Server.MapPath(@"~\imgs\testpic.PNG");
    Byte[] bytes = File.ReadAllBytes(path);
    string base64String = Convert.ToBase64String(bytes);

    return "data:image/png;base64," + base64String;
}

[...]

var imgUrl = ImageToBase64();
var cardImages = new List<CardImage>();
cardImages.Add(new CardImage(url: imgUrl));

var heroCard = new HeroCard
{
    Title = "BotFramework Hero Card",
    Subtitle = "Microsoft Bot Framework",
    Text = "Build and connect intelligent bots to interact with your users naturally wherever they are," +
            " from text/sms to Skype, Slack, Office 365 mail and other popular services.",
    Images = cardImages,
    Buttons = new List<CardAction> { new CardAction(ActionTypes.OpenUrl, "Get Started", value: "https://docs.microsoft.com/bot-framework") },
};

var attachment = heroCard.ToAttachment();
var message = MessageFactory.Attachment(attachment);
await context.PostAsync(message);