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)
})
}
}```
-
Describe 'not successful'Evert– Evert2024年12月24日 04:16:03 +00:00Commented 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 openfinJames– James2024年12月24日 14:07:12 +00:00Commented 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.Evert– Evert2024年12月24日 23:07:57 +00:00Commented 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.Evert– Evert2024年12月24日 23:10:44 +00:00Commented Dec 24, 2024 at 23:10