Microsoft Graph SDK 客户端配置了一组默认中间件,允许 SDK 与 Microsoft Graph 终结点通信。 此默认集是可自定义的,允许你更改客户端的行为。 例如,可以插入自定义日志记录,或添加测试处理程序来模拟特定方案。 可以添加和删除中间件组件。 请务必注意,中间件组件的运行顺序非常重要。
C#
// using Azure.Identity;
// https://docs.microsoft.com/dotnet/api/azure.identity.interactivebrowsercredential
var interactiveCredential = new InteractiveBrowserCredential(...);
var authProvider = new TokenCredentialAuthProvider(
interactiveCredential, scopes);
var handlers = GraphClientFactory.CreateDefaultHandlers(authProvider);
// Remove a default handler
var compressionHandler =
handlers.Where(h => h is CompressionHandler).FirstOrDefault();
handlers.Remove(compressionHandler);
// Add a new one
// ChaosHandler simulates random server failures
handlers.Add(new ChaosHandler());
var httpClient = GraphClientFactory.Create(handlers);
var customGraphClient = new GraphServiceClient(httpClient);
var messages = await customGraphClient.Me.Messages.Request()
.Top(100)
.Select(m => m.Subject)
.GetAsync();
TypeScript
const {
TokenCredentialAuthenticationProvider
} = require("@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials");
const {
AuthorizationCodeCredential
} = require("@azure/identity");
const credential = new AuthorizationCodeCredential(
"<YOUR_TENANT_ID>",
"<YOUR_CLIENT_ID>",
"<AUTH_CODE_FROM_QUERY_PARAMETERS>",
"<REDIRECT_URL>"
);
const authProvider = new TokenCredentialAuthenticationProvider(credential, {
scopes: [scopes]
});
// Create an authentication handler that uses custom auth provider
const authHandler = new MicrosoftGraph.AuthenticationHandler(authProvider);
// Create a custom logging handler
const loggingHandler = new CustomLoggingHandler();
// Create a standard HTTP message handler
const httpHandler = new MicrosoftGraph.HTTPMessageHandler();
// Use setNext to chain handlers together
// auth -> logging -> http
authHandler.setNext(loggingHandler);
loggingHandler.setNext(httpHandler);
// Pass the first middleware in the chain in the middleWare property
const client = MicrosoftGraph.Client.initWithMiddleware({
defaultVersion: 'v1.0',
debugLogging: true,
middleware: authHandler,
});
const response: PageCollection = await client
.api('/me/messages?$top=10&$select=sender,subject')
.get();
CustomLoggingHandler.ts
import { Context, Middleware } from "@microsoft/microsoft-graph-client";
export default class CustomLoggingHandler implements Middleware {
private nextMiddleware: any = null;
execute = async (context: Context): Promise<void> => {
console.log(`Logging request: ${context.request.toString()}`);
return await this.nextMiddleware.execute(context);
}
setNext = (middleware: Middleware): void => {
this.nextMiddleware = middleware;
}
}
Java
import com.azure.identity.InteractiveBrowserCredential;
import com.azure.identity.InteractiveBrowserCredentialBuilder;
import com.microsoft.graph.authentication.TokenCredentialAuthProvider;
import com.microsoft.graph.httpcore.HttpClients;
import okhttp3.OkHttpClient;
final List<String> scopes = Arrays.asList("User.Read");
final InteractiveBrowserCredential credential =
new InteractiveBrowserCredentialBuilder()
.clientId("clientId")
.redirectUrl("redirectUrl")
.build();
final TokenCredentialAuthProvider authProvider =
new TokenCredentialAuthProvider(scopes, credential);
// you can configure any OkHttpClient option and add interceptors
// Note: com.microsoft.graph:microsoft-graph:3.0 or above is required
// for a complete description of available configuration options https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/-builder/
final OkHttpClient httpClient = HttpClients.createDefault(authProvider)
.newBuilder()
.followSslRedirects(false) // sample configuration to apply to client
.build();
final GraphServiceClient graphServiceClient = GraphServiceClient
.builder()
.httpClient(httpClient)
.buildClient();
Go
Important: Microsoft Graph SDK for Go 目前处于预览状态。 不支持在生产环境中使用此 SDK。
import (
a "github.com/microsoft/kiota-authentication-azure-go"
khttp "github.com/microsoft/kiota-http-go"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
core "github.com/microsoftgraph/msgraph-sdk-go-core"
)
// Auth provider
auth, err := a.NewAzureIdentityAuthenticationProviderWithScopes(...)
// Get default middleware from SDK
defaultMiddleware := core.GetDefaultMiddlewaresWithOptions(msgraphsdk.GetDefaultClientOptions())
// Get instance of custom middleware
// Implement a custom middleware by implementing the Middleware interface
// https://github.com/microsoft/kiota-http-go/blob/main/middleware.go
allMiddleware := append(defaultMiddleware, mycustom.NewCustomHandler())
// Create an HTTP client with the middleware
httpClient := khttp.GetDefaultClient(allMiddleware...)
// Create the adapter
// Passing nil values causes the adapter to use default implementations
adapter, err :=
msgraphsdk.NewGraphRequestAdapterWithParseNodeFactoryAndSerializationWriterFactoryAndHttpClient(
auth, nil, nil, httpClient)
client := msgraphsdk.NewGraphServiceClient(adapter)
为客户端配置 HTTP 代理
某些环境要求客户端应用程序在访问公共 Internet 之前使用 HTTP 代理。 本部分介绍如何为 Microsoft Graph SDK 配置代理。
C#
// URI to proxy
var proxyAddress = "http://localhost:8888";
// Create a new System.Net.Http.HttpClientHandler with the proxy
var handler = new HttpClientHandler
{
// Create a new System.Net.WebProxy
// See WebProxy documentation for scenarios requiring
// authentication to the proxy
Proxy = new WebProxy(new Uri(proxyAddress))
};
// Create an options object for the credential being used
// For example, here we're using a ClientSecretCredential so
// we create a ClientSecretCredentialOptions object
var options = new ClientSecretCredentialOptions
{
// Create a new Azure.Core.HttpClientTransport
Transport = new HttpClientTransport(handler)
};
var credential = new ClientSecretCredential(
"YOUR_TENANT_ID",
"YOUR_CLIENT_ID",
"YOUR_CLIENT_SECRET",
options
);
var scopes = new[] { "https://graph.microsoft.com/.default" };
// This example works with Microsoft.Graph 4+
var httpClient = GraphClientFactory.Create(new TokenCredentialAuthProvider(credential, scopes), proxy: new WebProxy(new Uri(proxyAddress)));
var graphClient = new GraphServiceClient(httpClient);
TypeScript
// Create a credential from @azure/identity package
const credential = new ClientSecretCredential(
'YOUR_TENANT_ID',
'YOUR_CLIENT_ID',
'YOUR_CLIENT_SECRET',
{
proxyOptions: {
host: 'localhost',
port: 8888,
// If proxy requires authentication
//username: '',
//password: ''
},
}
);
// Create a Graph token credential provider
const tokenAuthProvider = new TokenCredentialAuthenticationProvider(
credential,
{
scopes: [ 'https://graph.microsoft.com/.default' ]
});
const client = MicrosoftGraph.Client.initWithMiddleware({
authProvider: tokenAuthProvider,
// Configure proxy in fetchOptions
fetchOptions: {
agent: new HttpsProxyAgent('http://localhost:8888')
}
});
Java
final int proxyPort = 8080;
final InetSocketAddress proxyInetAddress = new InetSocketAddress("proxy.ip.or.hostname", proxyPort);
// The section below configures the proxy for the Azure Identity client
// and is only needed if you rely on Azure Identity for authentication
final ProxyOptions pOptions = new ProxyOptions(ProxyOptions.Type.HTTP, proxyInetAddress);
pOptions.setCredentials("username", "password");
final HttpClientOptions clientOptions = new HttpClientOptions();
clientOptions.setProxyOptions(pOptions);
final HttpClient azHttpClient = HttpClient.createDefault(clientOptions);
// Or any other credential the application is using
final ClientSecretCredential clientSecretCredential =
new ClientSecretCredentialBuilder()
.clientId(CLIENT_ID)
.clientSecret(CLIENT_SECRET)
.tenantId(TENANT_GUID)
// don't forget that addition to use the configured client
.httpClient(azHttpClient)
.build();
final TokenCredentialAuthProvider authenticationProvider =
new TokenCredentialAuthProvider(Arrays.asList(SCOPES), clientSecretCredential);
// The section below configures the proxy for the Microsoft Graph SDK client
final Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyInetAddress);
// This block is only needed if the proxy requires authentication
final Authenticator proxyAuthenticator = new Authenticator() {
@Override
public Request authenticate(Route route, Response response) throws IOException {
String credential = Credentials.basic("username", "password");
return response.request().newBuilder()
.header("Proxy-Authorization", credential)
.build();
}
};
final OkHttpClient graphHttpClient =
HttpClients.createDefault(authenticationProvider)
.newBuilder()
.proxy(proxy)
.proxyAuthenticator(proxyAuthenticator)
.build();
final GraphServiceClient graphServiceClient =
GraphServiceClient
.builder()
.httpClient(graphHttpClient)
.buildClient();
Note: 有关 Azure 标识代理配置的详细信息,请 参阅 ProxyOptions。
Go
Important: Microsoft Graph SDK for Go 目前处于预览状态。 不支持在生产环境中使用此 SDK。
Microsoft Graph SDK for Go 当前不支持 HTTP 代理。 有关更多详细信息,请参阅此GitHub问题。
历史资料。此文对应 2023-02-03 的中文文档快照,适用产品版本和命令行为以原文为准。
来源与署名:Microsoft 和文档贡献者(原始作者元数据 DarrelMiller)。固定版本原文。文档采用 CC BY 4.0,示例代码采用 MIT 授权。本站将 Markdown 转为 HTML,规范化链接,移除外部媒体和导航标记。
