我使用图形教程创建了一个示例 angular 项目。我的要求是将 Microsoft 团队集成到一个有角度的应用程序中。使用示例项目,我可以使用微软凭据登录。以下代码用于获取访问令牌。
async getAccessToken(): Promise<string> {
const result = await this.msalService
.acquireTokenSilent({
scopes: OAuthSettings.scopes
})
.toPromise()
.catch((reason) => {
this.alertsService.addError('Get token failed', JSON.stringify(reason, null, 2));
});
if (result) {
return result.accessToken;
}
// Couldn't get a token
this.authenticated = false;
return '';
}
这是我的 Oauth 设置
export const OAuthSettings = {
appId: Application (client) ID from Azure portal,
redirectUri: 'http://localhost:4200',
scopes: [
"user.read",
"mailboxsettings.read",
"calendars.read",
"OnlineMeetings.ReadWrite"
]
};
这是用于创建会议的代码
private graphClient: Client;
constructor(private authService: AuthService) {
// Initialize the Graph client
this.graphClient = Client.init({
authProvider: async (done) => {
// Get the token from the auth service
const token = await this.authService.getAccessToken()
.catch((reason) => {
done(reason, null);
});
if (token)
{
done(null, token);
} else {
done("Could not get an access token", null);
}
}
});
}
async createCall() {
const onlineMeeting = {
startDateTime: '2021-07-20T07:30:34.2444915-07:00',
endDateTime: '2021-07-20T15:30:34.2444915-07:00',
subject: 'User Token Meeting'
};
await this.graphClient.api('/me/onlineMeetings').post(onlineMeeting);
}
但是当我调用上面的函数 createCall() 时,我得到 400 和以下是响应
{
"error": {
"code": "AuthenticationError",
"message": "Error authenticating with resource",
"innerError": {
"date": "2021-07-20T00:37:47",
"request-id": request-id,
"client-request-id": client-request-id
}
}
}
令牌api也被调用,
Request URL: https://login.microsoftonline.com/common/oauth2/v2.0/token
这给出了响应
{
"token_type": "Bearer",
"scope": "User.Read MailboxSettings.Read Calendars.Read openid profile",
"expires_in": 3600,
"ext_expires_in": 3600,
"access_token": access_token,
"refresh_token": refresh_token,
"id_token": id_token,
"client_info": client_info
}
所以在上面的回复中没有显示我在 Oauth 设置中传递的在线会议范围。
这是在 Azure 门户中授予应用程序的 api 权限