dfreelon / fb_scrape_public

Scrapes posts and comments from public Facebook pages.
BSD 3-Clause "New" or "Revised" License
106 stars 52 forks source link

Token not approved? #9

Closed ianbstewart closed 6 years ago

ianbstewart commented 6 years ago

I recently tried using this code to query Facebook using my access token, which I have previously used to scrape data. I only got an error with the following message:

{"error":{"message":"(#10) To use 'Read Groups Content Without User Data', your use of this endpoint must be reviewed and approved by Facebook. To submit this 'Read Groups Content Without User Data' feature for review please read our documentation on reviewable features: https:\/\/developers.facebook.com\/docs\/apps\/review.","type":"OAuthException","code":10,"fbtrace_id":"FLXFsqHLk5Q"}}

Do you know if this is a recent change?

dfreelon commented 6 years ago

See the readme--you can only scrape Facebook pages with user tokens now, which only last two hours. :-(

ianbstewart commented 6 years ago

Oh that's frustrating! Thanks for clearing it up. I wish they didn't make it so hard!

ianbstewart commented 6 years ago

Update: I tried generating the user token and still got the same authentication error. Code below:

import urllib.requests
import json
client_id = 'XXX' # fill in with actual client id/secret
client_secret = 'YYY'
# generate access token
access_token_generate_url = 'https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=%s&client_secret=%s'%(client_id, client_secret)
access_token = json.loads(urllib.request.urlopen(access_token_generate_url).read().decode(encoding='latin1'))['access_token']
# scrape comments
page_id = 'barackobama'
scrape_url = 'https://graph.facebook.com/v3.0/%s?fields=from,message,id,created_time&access_token=%s'%(page_id, access_token)
test_output = urllib.request(scrape_url) # result: urllib.error.HTTPError: HTTP Error 400: Bad Request

Are you able to do this basic scrape successfully? It might have something to do with my credentials being out of date.

dfreelon commented 6 years ago

That's not gonna work--Facebook doesn't let users generate tokens automatically. Try something like this:

obama_posts = fsp.scrape_fb(token="YourAccessToken",ids="barackobama",outfile='obama_posts.csv')

Except replace YourAccessToken with a token generated manually from this page: https://developers.facebook.com/tools/explorer/

That worked for me in May; who knows if it'll work now.

ianbstewart commented 6 years ago

Thanks for the feedback. I just generated a token, ran that line of code and got another "400: Bad Request" result:

HTTP Error 400: Bad Request
Skipping ID barackobama ...
Script completed in 0.293956995010376 seconds.

What scopes does your access token have? Maybe the reason that my token isn't working is that it doesn't have the right scopes. Mine has:

user_events, read_insights, user_managed_groups, manage_pages, pages_manage_cta, pages_manage_instant_articles, pages_show_list, publish_pages, read_page_mailboxes, ads_management, ads_read, business_management, pages_messaging, pages_messaging_phone_number, pages_messaging_subscriptions, publish_to_groups, groups_access_member_info, public_profile

dfreelon commented 6 years ago

Doesn't work for me either. Facebook may have closed that loophole. Now all you can do is try to get business verification to re-enable your app credentials: https://developers.facebook.com/docs/apps/review#business-verification

ianbstewart commented 6 years ago

OK, I'll look into that. Thanks for corroborating the access token bug.