stackexchange-api

Stack Exchange API with curl command


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?


Solution

  • 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.