Can't get retrun response from /api/v2/users/<user_id>.json API | Community
Skip to main content

Can't get retrun response from /api/v2/users/<user_id>.json API

  • November 18, 2022
  • 7 replies
  • 0 views

Vishwanath

From my react application, I'm  calling below function i.e. javascript function

export const getUserName=async(author_id,login,password)=>{

return await axios.get(baseURL+`/api/v2/users/${author_id}.json`, {
                   headers: {'Access-Control-Allow-Origin': '*'}
auth:  {username: login,password: password}})
              .then(response=>console.log(response))
}

After hitting above method, getting below error 

Access to XMLHttpRequest at '<baseURL>/api/v2/users/<id>.json' 
from origin 'http://localhost:3000' has been blocked by CORS policy:
Request header field access-control-allow-origin is not allowed by
Access-Control-Allow-Headers in preflight response.

 

If I hit same url in postman, its working fine where as its not working from react-application. 

Let me know if you required any further details. 

7 replies

Tipene
  • November 18, 2022
Hi Vishwanath,
 
Since you're making a client-side request to the API, you will need to authenticate with an Oauth token in order for CORS to be implemented. Here's a link to article which goes in to more detail:

https://developer.zendesk.com/documentation/ticketing/using-the-zendesk-api/making-cross-origin-browser-side-api-requests/
 
I hope this helps!
 
Tipene

Vishwanath
  • Author
  • November 21, 2022

Hi Tipene,

I'm not yet deployed my client application in produciont, so building in my local system.

In this case, what should be the redirect url as i don't have registered domain name yet.

is it http://127.0.0.1 or https://<ip address my local system>

 

Tipene
  • November 21, 2022
Hi Vishwanath,
 
The redirect URL is where the user will be sent after access is granted to the app so if you'd like them to be sent to the root page of your app, http://127.0.0.1 or http://localhost:{port} should work just fine.

Vishwanath
  • Author
  • November 22, 2022

Hi Tipene,

I have setup same thing, but still getting same error message. 

In 'OAuth Clients' tab, setup as below:

Below code used :

 

  export const getUserName=async(author_id,login,password)=>{
                  const access_token='<token from UI generated in 'OAuth Clients' tab>';
                    var request = new XMLHttpRequest();
                  var url = '<baseURL>'+`/api/v2/users/${author_id}.json`;
                    request.open('GET', url, true);
                    request.setRequestHeader("Authorization", "Bearer " + access_token);
                    request.send()
}

Let me know if you need further information. 

Tipene
  • November 22, 2022
Hi Vishwanath,
 
Can you confirm that the authorization flow is being run successfully and you're seeing the access token returned in the redirect URL?

Vishwanath
  • Author
  • November 23, 2022

Hi Tipene,

No, authorization flow is not run successful and token is not returned in redirect URL. 

If you need further information, i will setup demo to you. 


Tipene
  • November 23, 2022
Hi Vishwanath,
 
Thanks for letting me know.
 
I'm going to create a ticket so we can take a look at your implementation in a bit more detail. You should see an email coming through from me shortly.
 
Thanks!
 
Tipene