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



Publicado 18 nov 2022

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. 


0

7

7 comentarios

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

0


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. 

0


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?

0


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. 

0


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.

0


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>

 

0


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

0


Iniciar sesión para dejar un comentario.

¿No encontró lo que buscaba?

Nueva publicación