【问题标题】:Pass access token as URL query parameter?将访问令牌作为 URL 查询参数传递?
【发布时间】:2019-10-29 03:42:42
【问题描述】:

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

我在 CardImage 的构造函数中传递了图像 URL。没有其他构造函数或函数可以修改它,所以我无法像往常一样获取带有令牌的图像,然后将其发送到 CardImage。

有没有办法将令牌作为 url 查询字符串传递?我知道不建议这样做,那么还有其他方法可以完成吗? 感谢您的帮助

【问题讨论】:

    标签: c# botframework microsoft-graph-api


    【解决方案1】:

    我不相信有办法做到这一点,除非您以某种方式拦截客户端中的 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();
    }
    

    现在我们有了令牌。

    第二步

    // 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;
        }
    }
    

    第三步

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-10
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      相关资源
      最近更新 更多