4

By following this Stack Exchange API documentation, I have registered my application on Stack Apps (here). After registering I got my request key and access_tokens created via authentication.

I need to run the Stack Exchange API via curl command, for that, I ran the following command

curl "https://api.stackexchange.com/2.2/answers/43919322?order=desc&sort=activity&site=stackoverflow&access_token=my-access-token)EuzzA((&key=5RseNTJmS0C35DTMsc2dYQ((" | gunzip

After hitting the above request, I get the following error :

{"error_id":403,"error_message":"`key` is not valid for passed `access_token`, token not found.","error_name":"access_denied"}

For authentication, I referred this and hit the URL https://stackoverflow.com/oauth/dialog with query string parameters, but here also no response is shown.

Can anyone please tell me why this error is coming, even though the parameters mentioned in the curl request are correct OR is there any step left for authentication?

20
  • 2
    I think that your curl command is correct. About this, you can test by curl "https://api.stackexchange.com/2.2/answers/43919322?order=desc&sort=activity&site=stackoverflow" | gunzip. From your error message, I think that the key is correct and the access token is not correct. So can you confirm your access token again? From your question, it seems that you use the implicit OAuth 2.0 flow. In this case, this thread might be useful. stackapps.com/a/6679/44311 If this was not the direct solution, I apologize. Commented Jun 26, 2020 at 1:43
  • 2
    Thank you for replying. I apologize for the inconvenience. I think that the reason of your issue is your access token. In my environment, I could confirm that the access token retrieved with both explicit and implicit OAuth 2.0 flows worked. So can you provide the values of scope and redirect_uri of https://stackoverflow.com/oauth/dialog?client_id=###&scope=###&redirect_uri=### for retrieving the access token using the implicit OAuth 2.0 flow? By this, I would like to confirm whether I can replicate your situation. Commented Jun 26, 2020 at 2:14
  • 1
    Thank you for replying. If you are using https://stackoverflow.com/oauth/dialog?client_id=###&scope=no_expiry&redirect_uri=stackexchange.com, I think that please modify redirect_uri from stackexchange.com to http://localhost. And, access it. By this, when you see the URL of the browser, you can see http://localhost/#access_token=###. In this case, ### is the access token. How about this? Commented Jun 26, 2020 at 2:45
  • 2
    Thank you for replying. In that case, how about setting localhost as OAuth Domain? Or, if you want to use stackexchange.com, please try the URL of https://stackoverflow.com/oauth/dialog?client_id=###&scope=no_expiry&redirect_uri=http://stackexchange.com. Commented Jun 26, 2020 at 2:57
  • 1
    Thank you for replying. Yes. When you can see the URL of the browser like http://localhost/#access_token={accessToken}, please use {accessToken} as the access token. In my environment, I could confirm that the retrieved access token can be used for your curl command. If that was not the direct solution, I apologize. Commented Jun 26, 2020 at 3:07

1 Answer 1

5

Based on the discussion done above and the solution given by @Tanaike I was able to resolve the issue.

In order to run the Stack Exchange API with the curl command, we need to follow the following steps. To get a detailed explanation refer to this official documentation of Stack Exchange API.

Steps for Authentication

  1. Applications should be registered on Stack Apps to get a request key. Request keys grant more requests per day and are necessary for using access_tokens created via authentication.

  2. To register your Application (Click on Stack Apps), write the Application name, Description and in OAuth Domain write stackexchange.com.

    Refer to this answer, to get details about registering an application.

  3. After registering the application, the client ID, client secret and API key will be generated.

  4. Now hit URL https://stackoverflow.com/oauth/dialog?client_id=###&scope=no_expiry&redirect_uri=http://stackexchange.com from the browser. This will redirect to http://stackexchange.com/#access_token=###, where ###` is the access token.

  5. Now run this curl command with authentication parameters

    curl "https://api.stackexchange.com/2.2/answers/43919322?order=desc&sort=activity&site=stackoverflow&access_token=my-access-token&key=my-key" | gunzip
    
  6. After running the above curl command, the response will be generated.

Sign up to request clarification or add additional context in comments.

2 Comments

@Tanaike I have summarized the above discussion in this answer that helped me resolve the issue. Please have a look at this
I had to add --header "Accept-Encoding: gzip" or remove | gunzip to get this to work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.