fbsamples / threads_api

This repository contains a sample app for developers who are interested in integrating with the Threads API.
Other
191 stars 30 forks source link

UserId is different between authentication API and /me graph API #9

Closed Mikescops closed 4 months ago

Mikescops commented 4 months ago

Hello,

I'm getting a strange behaviour when I'm calling oauth/access_token and then the graph API /me.

As you can see below the two IDs are different by 1.

The id returned by /me is the correct one as it allows me to fetch the other resources associated to the user, the one provided by authentication is wrong and doesn't work.

image

Hope you can help on this one, thanks!

Mikescops commented 4 months ago

Sorry, don't bother, the userId is out of the JS integer space that is use by JSON.parse(), this causes the final value to be inconsistent when stored in memory. One solution would be to have the API return a string for the userId to avoid such scenario in most languages, but it can be solved on the client side too, so up to you.

Mikescops commented 4 months ago

Further reading for anyone stumbling upon this issue: https://jsoneditoronline.org/indepth/parse/why-does-json-parse-corrupt-large-numbers/

JSON.parse is used in most modern request libraries like got so make sure you parse the JSON manually instead.

Simplest solution here to replace the bigint into a string .replace(/([\[:])?(\d+)([,\}\]])/g, '$1"$2"$3');

pestevez commented 4 months ago

Thanks for the report. We will look at potential solutions. Since changing the type of the user_id field is considered a breaking change we will need some time, though.

In addition to the workaround of manually parsing the JSON, I can think about a couple more:

  1. Replacing the use of the user_id in the API request URLs with the keyword me.
  2. Storing in the session the result of the /me request, which is returned as a string.

We will implement the two solutions above soon in this sample app.

Mikescops commented 4 months ago

Thanks @pestevez, it wasn't crystal clear to me that me would work for every endpoint, maybe this could be added to the documentation.