1

I've done the OAuth process with Stack Exchange and I have my access token.

I'm trying to perform this request https://api.stackexchange.com/docs/me-posts#order=desc&sort=activity&filter=default&site=stackoverflow&run=true

If I put my access token on this web page I get:

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

If I perform my request in the app I get:

{
   "error_id":401,
   "error_message":"This method requires an access_token",
   "error_name":"access_token_required"
}

I'm passing my token as:

"Authorization": "Bearer \(token)"

in my headers. But I've also tried different approach such as:

"access_token": token
"Authorization": "Basic \(token)"

But nothing to do.

But if I pass my token in this request => https://api.stackexchange.com/docs/read-access-tokens

The response is correct with user id and my scopes.

So the question is: what's wrong with that? How and where I pass my access token? I tried to delete app and get a new token.

P.S.: I'm observing the same problem in a POST request. What's wrong?

UPDATE:

Post request => https://api.stackexchange.com/docs/favorite-question

My URL => https://api.stackexchange.com/2.2/questions/< QUESTIONID >/ favorite&access_token=< ACCESSTOKEN >&key=< KEY >

UPDATE TWO:

This is my body:

 {"access_token":"MYTOKEN","key":"MY KEY"} 

This is my Content-Type:

 request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")

This is my URL:

https://api.stackexchange.com/2.2/questions/MYQUESTIONID/favorite&site=stackoverflow
7
  • 4
    Can't tell what you're doing but it looks like you either do site=stackoverflow&access_token=key or something similar when you post data. access_token doesn't go in the header but as a 'formfield' in the body (or as query string parameter on the url). – rene Dec 14 '20 at 20:33
  • @rene thanks for your answer. It works for some request but I'm still having trouble with, for example, this one => api.stackexchange.com/docs/favorite-question. Where should I pass token and key in this POST? I'm adding it to the URL, but I get "no method found with this name", so I try to add them in HTTP body but same results. I update my question with the URL. – manubrio Dec 19 '20 at 12:08
  • No, that endpoint is tagged with Post (the blue tag in the title) so you need to provide all parameters in the post body, as content-type application/x-www-form-urlencoded – rene Dec 19 '20 at 12:39
  • @rene i updated one more time my question, I'm sorry for that but I cant understand how it works. What's wrong with my code? Im still getting "no method found with this name" – manubrio Dec 19 '20 at 12:46
  • all parameters need to go into body, including the site parameter. And I'm unsure what programming language you're using so can't tell what is wrong but your http body should look like: id=1&filter=default&key=U4DMV*8nvpm3EOpvf69Rxw((&site=stackoverflow – rene Dec 19 '20 at 12:57
  • Mmm i can't understand that much from doc since question id is embedded in the URL. At the moment my HTTP body looks like this => {"key":"KEY","access_token":"TOKEN","id":"ID","site":"stackoverflow"} But still getting no method found. This is my URL now =>api.stackexchange.com/2.2/questions/downvote – manubrio Dec 19 '20 at 13:08
  • 1
    Your last body is JSON, but the API expects application/x-www-form-urlencoded, e.g. what's in rene's last comment. – Glorfindel Dec 19 '20 at 13:36

You must log in to answer this question.

Browse other questions tagged .