0

Hi I need to send an post request to a url like example.com with auth2.0 token . I need to send the below details in the request just like we do in a postman for a auth2.0 request.

Token Name

Grant type

Callback URL http://your-application.com/registered/callback

Auth URL https://example.com/login/oauth/authorize

Access Token URL https://example.com/login/oauth/access_token

Client ID 1234

Client Secret: shshshhsss

Scope e.g. read:org

Client Authentication: Send as Basic authentication header

I wrote the below code. But the post call is not successful. Can someone tell me what is wrong. Thank you in advance


@NgModule({
 imports: [
 OAuthModule.forRoot({
 resourceServer: {
 sendAccessToken: true,
 },
 }),
 ],
})
export class AppModule {}
import { OAuthService } from 'angular-oauth2-oidc';
@Component({ ... })
export class AppComponent {
 constructor(private oauthService: OAuthService) {
 }
 ngOnInit() {
 let authConfig: AuthConfig = {
 grantType: 'authoriZationCode',
 scope: 'openid profile email',
 tokenName: 'sub',
 callBackUrl: window.location.origin,
 authUrl: 'https://example.com/login/oauth/authorize'
 accessTokenUrl: 'https://example.com/login/oauth/access_token',
 oidc: true,
 requestAccessToken: true,
 showDebugInformation: true,
 strictDiscoveryDocumentValidation: false,
 useSilentRefresh: true,
 clientId: environment.KEYCLOAK_CLIENT_ID,
 clientSecret: '1424242',
 clientAuthentication: 'basic'
 };
 this.oauthService.configure(authConfig);
 this.oauthService.setupAutomaticSilentRefresh();
 this.oauthService.loadDiscoveryDocumentAndTryLogin();
 this.oauthService.initCodeFlow();
 const accessToken = this.oauthService.getAccessToken();
 const headers = new Headers({
 'Content-Type': 'application/json',
 'Authorization': `Bearer ${accessToken}`
 })
 const apiUrl = 'example.com/post'
 return this.http.post(apiUrl, { headers: headers }).subscribe((data)=> {
 console.log(data)
 })
 }
 
 
}```
asked Dec 23, 2024 at 18:25
4
  • Describe 'not successful' Commented Dec 24, 2024 at 4:16
  • The call fails with cors error, Where as in postman it was successful, I cannot disable cors in my application as it runs on openfin Commented Dec 24, 2024 at 14:07
  • If you know why it failed and got a specific error, it seems kind of absurd to not include that information in your question. CORS is not something you need to disable in that case, you need to enable CORS headers if talk to a server on a different origin. Commented Dec 24, 2024 at 23:07
  • This question is similar to: Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, while Postman does not?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Dec 24, 2024 at 23:10

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.