【发布时间】: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”
提前感谢您的帮助
【问题讨论】: