【问题标题】:The user presence return value is unreadable用户存在返回值不可读
【发布时间】:2021-04-16 10:07:51
【问题描述】:

我是java新手,几个星期以来我一直试图通过microsoft graph报告一个人的状态,我没有错误,但返回的值是不可读的。我必须缺少一些东西来显示状态。 这是 Eclipse 中的 Gradle 项目 这是我的代码:

   ```public class App {
public static void main(String[] args) {


    // <LoadSettingsSnippet>
    // Load OAuth settings
    final Properties oAuthProperties = new Properties();
    try {
        oAuthProperties.load(App.class.getResourceAsStream("oAuth.properties"));
    } catch (IOException e) {
        System.out.println("Unable to read OAuth configuration. Make sure you have a properly formatted oAuth.properties file. See README for details.");
        return;
    }

    final String appId = oAuthProperties.getProperty("app.id");
    final List<String> appScopes = Arrays
        .asList(oAuthProperties.getProperty("app.scopes").split(","));
    // </LoadSettingsSnippet>

    // Initialize Graph with auth settings
    Graph.initializeGraphAuth(appId, appScopes);
    final String accessToken = Graph.getUserAccessToken();

    // getpresence
    Presence teamCollectionPage = Graph.getPresence();
    System.out.println("Valeur Presence : " + teamCollectionPage);
    
 

    Scanner input = new Scanner(System.in);

  

     

``

  public class Graph {

private static GraphServiceClient<Request> graphClient = null;
private static TokenCredentialAuthProvider authProvider = null;

public static void initializeGraphAuth(String applicationId, List<String> scopes) {
    // Create the auth provider
    final DeviceCodeCredential credential = new DeviceCodeCredentialBuilder()
        .clientId(applicationId)
        .challengeConsumer(challenge -> System.out.println(challenge.getMessage()))
        .build();

    authProvider = new TokenCredentialAuthProvider(scopes, credential);

    // Create default logger to only log errors
    DefaultLogger logger = new DefaultLogger();
    logger.setLoggingLevel(LoggerLevel.ERROR);

    // Build a Graph client
    graphClient = GraphServiceClient.builder()
        .authenticationProvider(authProvider)
        .logger(logger)
        .buildClient();
}

public static String getUserAccessToken()
{
    try {
        URL meUrl = new URL("https://graph.microsoft.com/v1.0/me");
        return authProvider.getAuthorizationTokenAsync(meUrl).get();
    } catch(Exception ex) {
        return null;
    }
}

public static Presence getPresence() {
    if (graphClient == null) throw new NullPointerException(
        "Graph client has not been initialized. Call initializeGraphAuth before calling this method");

    
    GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();

    Presence presence = graphClient.users("XXXXXXXXXXXXXXXX").presence()
        .buildRequest()
        .get();

    return presence ;
}

}

消息返回是“Valeur Presence : com.microsoft.graph.models.Presence@4d411036”

提前感谢您的帮助

【问题讨论】:

    标签: java microsoft-graph-api


    【解决方案1】:

    如果没有在子类中重写该方法(并且它不适用于 Presence 模型类),那么您看到的是默认值 Object.toString

    您可以显式访问该值,例如:

    System.out.println("Valeur Presence : " + teamCollectionPage.availability);
    

    【讨论】:

      猜你喜欢
      • 2019-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-30
      • 2013-05-22
      • 2016-06-16
      相关资源
      最近更新 更多