Angular oauth2.0 configure for post request

Discussão em 'Angular' iniciado por James, Dezembro 23, 2024.

  1. James

    James Guest

    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:eek:rg

    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)
    })
    }



    }```

    Continue reading...

Compartilhe esta Página