[英]I want to get my Graph API token but I got an error, I'm making a POST request with HttpServices. I'm getting a 400 status code. I'm using NestJS
**This is my code when I'm creating the class and the method:**
@Injectable()
export class MicrosoftGraph {
constructor(private httpService: HttpService) {}
getMicrosoftGraphToken() {
const data = {
client_id: 'myclientid',
scope: 'https://graph.microsoft.com/.default',
client_secret: 'myclientsecret',
grant_type: 'client_credentials'
}
const url =
'https://login.microsoftonline.com/mytenantid/oauth2/v2.0/token';
const response = this.httpService.post(url, data).pipe(
tap((resp) => console.log(resp)),
map((resp) => resp.data),
tap((data) => console.log(data)),
);
console.log(response);
return response;
}
}
This is the part of the controller:这是控制器的一部分:
@Controller('user')
export class UsersController {
constructor(private readonly microsoftGraph: MicrosoftGraph) {}
@Get('hola')
intento(@Res() res: Response, @Req() req: Request){
return this.microsoftGraph.getMicrosoftGraphToken();
}
}
And this is the error I'm getting:这是我得到的错误:
Observable {
source: Observable {
source: Observable {
source: [Observable],
operator: [Function (anonymous)]
},
operator: [Function (anonymous)]
},
operator: [Function (anonymous)]
}
[Nest] 19464 - 08/30/2022, 8:05:31 AM ERROR [ExceptionsHandler] Request failed with status code 400
[Nest] 19464 - 08/30/2022, 8:05:31 AM ERROR [ExceptionsHandler] undefined
I'm trying to get this token to be able to make a function where I can pull a list of users in my company, please help and thanks我正在尝试让这个令牌能够实现一个功能,我可以在其中拉出我公司的用户列表,请帮助并感谢
Not sure if you solved this already, but the problem is in your URL.不确定您是否已经解决了这个问题,但问题出在您的 URL 中。 You have hardcoded the tenantId part.您已经对 tenantId 部分进行了硬编码。 So instead of:所以不是:
const url = 'https://login.microsoftonline.com/mytenantid/oauth2/v2.0/token';
It should probably be something like this:它可能应该是这样的:
const graphTenantId = `1234-abcd-etc-etc`; const url = `https://login.microsoftonline.com/${graphTenantId}/oauth2/v2.0/token`;
The way you are currently doing it, is that you are trying to get the tenant with ID "mytenantid".您目前的做法是尝试获取 ID 为“mytenantid”的租户。
Also, since you are using NestJS, I would advise you to use the.env file to save the data that you now put in your "data" constant and similar to the tenant_id that you still need to add.此外,由于您使用的是 NestJS,我建议您使用 .env 文件来保存您现在放入“数据”常量中的数据,类似于您仍需要添加的 tenant_id。 This of course only applies when these values are set in stone.当然,这仅适用于这些值一成不变的情况。 The NestJS documentation has a great summary on how to do this. NestJS 文档对如何执行此操作有很好的总结。
If you have dynamic data in regards to the Graph API (so you would have clients that would connect through your NestJS application), you can probably best make a GraphConfiguration object which you then get using a service (that's the way I did it).如果你有关于 Graph API 的动态数据(所以你会有通过你的 NestJS 应用程序连接的客户端),你最好制作一个 GraphConfiguration 对象,然后你可以使用一个服务(我就是这样做的)。
Hope this helps you!希望这对你有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.