【发布时间】:2021-04-19 10:35:28
【问题描述】:
我按照 MS Graph SDK java 文档的说明进行操作,但出现错误:
Caused by: com.microsoft.graph.http.GraphServiceException: Error code: InvalidAuthenticationToken
Error message: Access token is empty.
当我使用 SDK 时,我不知道令牌有什么问题,因为我没有调用登录,而是我在 GraphServiceClient 中注入 tokenCredentialAuthProvider 来进行调用。
我的图表配置:
@Configuration
public class GraphConfig {
@Bean
GraphServiceClient<Request> graphServiceClient() {
String proxyHost = "proxy.company.com";
int proxyPort = 8080;
//PROXY CONF
final OkHttpClient httpClient = new OkHttpClient().newBuilder()
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)))
.build();
final ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
.clientId(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
.clientSecret(ssssssssssssssssssssssssssssssssssss)
.tenantId(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
.proxyOptions(new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)))
.build();
final TokenCredentialAuthProvider tokenCredentialAuthProvider =
new TokenCredentialAuthProvider(Collections.singletonList("https://graphContact.microsoft.com"), clientSecretCredential);
return GraphServiceClient
.builder()
.authenticationProvider(tokenCredentialAuthProvider)
.httpClient(httpClient)
.buildClient();
}
}
类使用客户端调用图与SDK
@Slf4j
@Component
public class GraphClient {
// Microsoft Graph Batch Limit
private static final Integer BATCH_LIMIT = 20;
// Microsoft Client
private final GraphServiceClient<?> graphServiceClient;
/**
* Constructor injecting properties for connection
*
* @param graphServiceClient client to deal wtih Microsoft Graph
*/
public GraphClient(GraphServiceClient<Request> graphServiceClient) {
this.graphServiceClient = graphServiceClient;
}
public Contact createContactsInFolder(Contact contact, String folderId, Sring user) {
// THIS METHOD RETURNS 401 Access token is empty
return graphServiceClient
.users(user)
.contactFolders(folderId)
.contacts()
.buildRequest()
.post(contact);
}
}
【问题讨论】:
标签: java sdk microsoft-graph-api token microsoft-graph-sdks