简体   繁体   English

我想获取我的 Graph API 令牌,但出现错误,我正在使用 HttpServices 发出 POST 请求。 我收到 400 状态码。 我正在使用 NestJS

[英]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.

相关问题 我收到了(请求失败,状态码为 500) - I'm getting (Request failed with status code 500) 请求不断获取错误代码:400-有人可以看看我在做什么错吗? - Request keeps on getting Error code: 400 - can someone please have a look on what I'm doing wrong? I'm trying to fetch my json site to enable API, but but got an error “Unexpected token &lt; in JSON at position 0” - I'm trying to fetch my json site to enable API, but but got an error “Unexpected token < in JSON at position 0” 我在 Formik 中使用 axios post 请求,并返回 400 错误 - i'm using axios post request in Formik,and return me 400 error Jquery REST API POST 调用,我收到 415 错误? - Jquery REST API POST call, I'm getting 415 error? 我想使用 fetch 方法从 api url 获取请求。 但我不断收到错误 400 - I want to get request from an api url using fetch method. But I keep on getting error 400 为什么我的SQL代码出现语法错误? - How come I'm getting a syntax error in my SQL code? 我收到了NaN错误 - I'm getting a NaN error 我在 Node.js API 上使用 Axios 没有收到 GET 请求的响应 - I'm getting no response of GET request with Axios on my Node.js API 为什么我在尝试使用 axios 发送发布请求时遇到网络错误 - Why i'm getting a network error, when trying to send a post request using axios
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM