在打字稿/反应中调用Microsoft图形函数

时间:2019-03-25 07:29:28

标签: reactjs graph

我是React和TypeScript的新手。我想在我的React应用程序中调用以下Microsoft图形API。

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

public class Main {

    public static void main(String[] args) {
        List<Customer> customers = Arrays.asList(new Customer(1, "A", 10, BigDecimal.valueOf(673.89)),new Customer(2, "B", 10, BigDecimal.valueOf(673.89)));
        getCustomer(customers).ifPresent(c -> System.out.println(c.getId()));
    }

    private static Optional<Customer> getCustomer(List<Customer> customers) {
        // @formatter:off
        return customers.stream()
                .max(
                        Comparator.comparing(Customer::getQuantity)
                        .thenComparing(Customer::getCost)
                         // here I need to check, If multiple customers are after 
                         // comparing the cost, I have to update the lastPurchasedDate l
                         // attribute value & then sort it
                        .thenComparing(Customer::getLastPurchasedDate)
                        );
        // @formatter:on
    }
}

我创建了此代码

https://graph.microsoft.com/v1.0/me/photo/$value

我收到 accessToken为空的错误。有没有一种方法可以在应用程序中填充令牌而无需在代码中动态设置令牌?

亲切的问候, 阿诺克

1 个答案:

答案 0 :(得分:0)

您可以使用我的方法 我首先获取访问令牌,然后调用查询从MS Graph获取数据 并可以在CSS类中设置

 var UrlImage;
    var that = this;    
      AuthContext.acquireToken("https://graph.microsoft.com", function (error, token) {
        var request = new XMLHttpRequest;
        request.open("GET", "https://graph.microsoft.com/beta/me/Photos/48X48/$value");
        request.setRequestHeader("Authorization", "Bearer " + token);
        request.responseType = "blob";

        request.onload = function () {
            if (request.readyState === 4 && request.status === 200) {

                UrlImage = request.response;
                //  var imageElm = document.createElement("img");
                var reader = new FileReader();
                reader.onload = function () {
                    // Add the base64 image to the src attribute
                    // imageElm.src = reader.result;
                    that.setState({ imgURL: reader.result },()=>{console.log('set url in state')})

                    // Display the user's profile picture
                    //document.getElementsByClassName('user-picture-box')[0].appendChild(imageElm);
                }
                reader.readAsDataURL(request.response);
            }
        };
       // debugger;
        console.log('request:', request)
        request.send(null);
    });