I'm trying to develop a web app locally using reddit api with angular 11 and I'm trying to get an auth token using the instructions from this link: Reddit OAuth2 docs
However, I'm running into a bit of a problem.
I've set up my headers to include the usual methods within a service such as:
httpOptions = {
headers: new HttpHeaders({
"Content-Type": "application/x-www-form-urlencoded",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, HEAD, OPTIONS,POST, PUT",
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept, x-client-key, x-client-token, x-client-secret, Authorization",
})
};
And I set up the function to retrieve the auth token within the service with:
getRedditAuthToken() {
console.log("Auth token");
return this.http.get<any>(this.redditAuthUrl, this.httpOptions);
}
The redditAuthUrl is set with the following parameters taken from reddit's oAuth documentation:
https://www.reddit.com/api/v1/authorize?client_id=CLIENT_ID&response_type=TYPE&
state=RANDOM_STRING&redirect_uri=URI&duration=DURATION&scope=SCOPE_STRING
Finally, the service is being retrieved by the component with:
getRedditAuthToken(): void {
this.redditService.getRedditAuthToken().subscribe((data) => {
console.log(data);
});
}
I'm getting the follow errors though when trying to access the auth token (found on web inspector):
Access to XMLHttpRequest at 'https://www.reddit.com/api/v1/authorize.compact?client_id=<client_id>&response_type=code&state=<random_string>&redirect_uri=http://localhost:4200/reddit&duration=temporary&scope=identity' from origin 'http://localhost:4200' has been blocked by CORS policy: Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response
GET https://www.reddit.com/api/v1/authorize.compact?client_id=<client_id>&response_type=code&state=<random_string>&redirect_uri=http://localhost:4200/reddit&duration=temporary&scope=identity net::ERR_FAILED
ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: "https://www.reddit.com/api/v1/authorize.compact?cl…ost:4200/reddit&duration=temporary&scope=identity", ok: false, …}
I've had several attempts at solutions like starting angular with proxy json file, modifying the httpHeaders, testing the other reddit oAuth token retrievals (using post method), but I'm always getting the same CORS issues.