Search code examples
angularoauth-2.0discord

Get Infos from Discord API using oauth2


First of all I'm very new to Angular 10.0 and oauth2 so I'm working on this to learn, so sorry in advance if something sound dumb.

My goal is to get at least the username of the person who logins using discord. For this I use this library

  const authConfig: AuthConfig = {
  loginUrl: `https://discord.com/api/oauth2/authorize`,
  tokenEndpoint: 'https://discord.com/api/oauth2/token',
  redirectUri: `http://localhost:4200`,
  clientId: 'myId',
  scope: 'identify',
  responseType: 'code',
  strictDiscoveryDocumentValidation: false,
  oidc: false,
  disablePKCE: false
};
this.oauthService.configure(authConfig);
this.oauthService.tryLoginCodeFlow();

connect(){
this.oauthService.initLoginFlow();
}

So this part works perfectly, I get the AccessToken but then anything I try to get infos like in example : console.log(this.oauthService.loadUserProfile());

I get this error : error loading user info TypeError: req.url is null

So I'm wondering if I have to do something else to connect to the API to get infos from it, but I can't figure out what i could be.

Best regards.


Solution

  • I resolved my issue,

    The thing with discord API is that it doesn't use OpenId Connect.

    But instead I had to use a GET request to apiLink+'/users/@me' while having my accessToken set with the authorize part.

    https://discord.com/developers/docs/resources/user#get-current-user