2

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?

ESCoder
  • 15,431
  • 2
  • 19
  • 42
  • 1
    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. https://stackapps.com/a/6679/44311 If this was not the direct solution, I apologize. – Tanaike Jun 26 '20 at 01:43
  • @Tanaike thanks for the reply. The curl command is working fine, but when authentication parameters are included, it shows up the error as mentioned in the question. The thread, which you mentioned above, following that I made the changes in the app, but still I am getting the same error as mentioned above I the question. – ESCoder Jun 26 '20 at 01:57
  • 1
    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. – Tanaike Jun 26 '20 at 02:14
  • @Tanaike The parameters are mentioned in this URL -> https://stackoverflow.com/oauth/dialog?client_id=18159&scope=no_expiry&redirect_uri=stackexchange.com . Can you please go through this. Client_id = 18159, OAuth Domain= stackexchange.com – ESCoder Jun 26 '20 at 02:37
  • 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? – Tanaike Jun 26 '20 at 02:45
  • @Tanaike for modifying redirect_uri from ```stackexchange.com``` to ```http://localhost```, do I need to make changes in the OAuth Domain (by editing the app)? And, if yes what should I set in OAuth Domain? – ESCoder Jun 26 '20 at 02:52
  • 1
    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`. – Tanaike Jun 26 '20 at 02:57
  • @Tanaike After hitting this URL -> ```https://stackoverflow.com/oauth/dialog?client_id=###&scope=no_expiry&redirect_uri=http://stackexchange.com```, I got access_token. Now are all the authentication steps completed ? And should I hit this ```curl "https://api.stackexchange.com/2.2/answers/43919322?order=desc&sort=activity&site=stackoverflow&access_token=my-access-token)EuzzA((&key=5RseNTJmS0C35DTMsc2dYQ((" | gunzip``` with the access token generated . – ESCoder Jun 26 '20 at 03:04
  • 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. – Tanaike Jun 26 '20 at 03:07
  • @Tanaike can I use the same curl command as mentioned in the question, with only changing the ```access token``` with the ```new access token``` that I got. Do I have to make the changes in the curl command as I think it is said to use ```GET requests for implicit (client-side) OAuth``` – ESCoder Jun 26 '20 at 03:13
  • 1
    Thank you for replying. I think that in your case, you can use the curl command for the retrieved access token and the corresponding key. – Tanaike Jun 26 '20 at 03:15
  • @Tanaike thank you so much for replying, I am using the following curl command ```curl "https://api.stackexchange.com/2.2/answers/43919322?order=desc&sort=activity&site=stackoverflow&access_token={access-token}EuzzA((&key={key}" | gunzip``` but still same error is coming. Is there something wrong in this curl command ? Or it would be of great help if you can suggest some other curl command through which I can just be sure that the authentication is done correctly . – ESCoder Jun 26 '20 at 03:26
  • Thank you for replying. What is `EuzzA((` of `access_token={access-token}EuzzA((`? – Tanaike Jun 26 '20 at 03:28
  • @Tanaike I am not sure about what ```EuzzA``` is, I have just picked up this curl command from somewhere to test whether authentication is properly configured, it would be of great help if you can just provide a basic format of curl command where I can put ```access_token``` and ```key``` – ESCoder Jun 26 '20 at 03:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/216688/discussion-between-bhavya-and-tanaike). – ESCoder Jun 26 '20 at 04:24
  • Thank you for replying. I have to apologize for my poor English skill. Unfortunately, I cannot understand about your replying. Can you explain about the detail of it? – Tanaike Jun 26 '20 at 04:36
  • @Tanaike thank you so much for your time. I was able to run the curl command with authentication parameters :) – ESCoder Jun 26 '20 at 14:53
  • @Tanaike can you please go through this question https://stackoverflow.com/questions/62597464/get-all-unaccepted-answers-within-a-date-range-at-user-level-using-stack-exchang – ESCoder Jun 26 '20 at 16:35
  • For your new question, I proposed a workaround for achieving your issue. Could you please confirm it? If that was not the direction you expect, I apologize. – Tanaike Jun 27 '20 at 01:14
  • @Tanaike the answer which you gave worked perfectly. Thank you once again :) – ESCoder Jun 27 '20 at 01:22

1 Answers1

3

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 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, 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 willl 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.

double-beep
  • 5,031
  • 17
  • 33
  • 41
ESCoder
  • 15,431
  • 2
  • 19
  • 42
  • 2
    @Tanaike I have summarized the above discussion in this answer that helped me resolve the issue. Please have a look at this – ESCoder Jun 27 '20 at 01:59