调用Facebook pass_thread_control接口遇Invalid OAuth access token错误求助
Hey there, let's break down why you're hitting that OAuthException (code 190) error and how to fix it.
First, the most obvious issue in your code is how you're handling the accesstoken variable:
- You defined it as a dictionary:
accesstoken = { "access_token": "######"} - Then you tried to plug this directly into the URL string with
.format(accesstoken). This means your final URL will haveaccess_token={'access_token': '######'}instead of just the raw token string. Facebook's API can't parse that messy format, hence the invalid token error.
Step-by-step fixes:
Correct the access token variable type
Change youraccesstokento store just the token string, not a dictionary:accesstoken = "######" # Replace with your actual valid access tokenUse a safer way to pass the access token
Instead of error-prone string formatting, userequests' built-inparamsparameter to pass the token. This handles URL encoding automatically and keeps your code cleaner:import json import requests passData = json.dumps({ "recipient": {"id": 627531511140634}, "target_app_id": 123456789, "metadata": "-X pass_thread_control X-" }) accesstoken = "######" passHeaders = {"Content-Type": "application/json"} hj = requests.post( "https://graph.facebook.com/v18.0/me/pass_thread_control", # Use a recent API version headers=passHeaders, data=passData, params={"access_token": accesstoken} ) # Print the full response to debug further if needed print(hj.json())Verify other critical details
- API Version: You're using v2.6, which is extremely outdated. Facebook regularly deprecates old API versions, so switch to a recent stable version (like v18.0 at the time of writing) to avoid compatibility issues.
- Token Validity: Ensure your access token is a valid Page Access Token with the
pages_messagingpermission. You can check its validity using Facebook's built-in Access Token Debugger tool. - Parameter Accuracy: Double-check that
recipient.idis a valid Page-Scoped ID (PSID) of the user, andtarget_app_idis the correct ID of the app you're transferring thread control to.
If you fix the access token handling first and still get errors, checking the full response body (like we did with print(hj.json())) will give you more specific clues about what's wrong.
内容的提问来源于stack exchange,提问作者jehanzaib

