如何通过Facebook主页URL或用户名获取用户ID?
Hey there, I totally get where you're coming from—this used to be a quick, no-fuss task, but Facebook has clamped down hard on API access for privacy reasons, which is exactly why that direct https://graph.facebook.com/username call is throwing the OAuthException #803 error you’re seeing. The post you came across is spot-on: Facebook no longer allows querying users directly by username via their public API.
That said, there are a few workarounds depending on whether you’re targeting a public Facebook Page or a personal profile:
Feasible Solutions
Extract ID from Embedded Page Plugins
For public Facebook Pages, use Facebook’s Page Plugin to generate an embed for the page. When you create the embed code, the page ID will be hidden in either thedata-hrefattribute or the accompanying JavaScript snippet. For example, the embed might include something likedata-href="https://www.facebook.com/PageName/?ref=embed_page"—you can inspect the embedded content’s metadata to pull out the ID, or use the plugin’s JS to access page details (just make sure you follow Facebook’s embedding guidelines).Scrape Public Page Metadata (Proceed With Caution)
Public pages often include their ID in HTML meta tags. If you fetch the page’s source code, look for tags like:<meta property="fb:page_id" content="1234567890" />or
<meta property="al:android:url" content="fb://page/1234567890" />A quick heads-up: Scraping Facebook’s pages might violate their Terms of Service, so only use this for personal, non-commercial purposes. Facebook actively blocks scraping tools, so this method could stop working at any time.
Use the Graph API with a Valid Access Token
If you have a valid access token (either a user token with appropriate permissions or an app token), you can use thesearchendpoint to look up pages (personal profiles are far more restricted here). The request would look like:https://graph.facebook.com/v19.0/search?q=YOUR_USERNAME&type=page&access_token=YOUR_TOKENFor personal profiles, this only works if the user has set their profile to be searchable and your app has been granted rare, specific permissions (Facebook is extremely strict about accessing personal user data now). For pages, this is more feasible if your app meets their requirements.
Check URL Redirects
Sometimes, visiting a username-based URL (likehttps://facebook.com/MyUsername) redirects to a URL that includes the ID, e.g.,https://www.facebook.com/profile.php?id=1234567890. You can send a HEAD request to the username URL and check theLocationheader for this redirected link, then extract the ID from theidparameter. Note that this won’t work for profiles/pages with strict privacy settings that disable direct redirects.
Critical Reminders
- Facebook’s policies and API restrictions change constantly—any workaround here could stop working overnight without warning.
- Always follow Facebook’s Terms of Service. Unauthorized scraping or API misuse can result in your account or app being banned.
- For personal user profiles, it’s increasingly hard to get the ID unless the user explicitly shares it with you, thanks to Facebook’s strict privacy controls.
内容的提问来源于stack exchange,提问作者Agent Smith

