pablobarbera / Rfacebook

Dev version of Rfacebook package: Access to Facebook API via R
http://cran.r-project.org/web/packages/Rfacebook
350 stars 250 forks source link

Add permissions and page token to get insights #107

Closed vanatteveldt closed 7 years ago

vanatteveldt commented 7 years ago

AFAIK, for most facebook insights you now need extra permission scope (read_insights) and/or a page token (which requires manage_pages scope).

The commits here add a scope= argument to the fbOAuth function to be able to get these extra permissions on the normal access token, and add the getPageToken function to get a page access token.

It's possible that the page token is not needed, but whenever I tried to get insights I got empty results until I got the page access token:

token = fbOAuth(app_id, app_secret, scope=c("read_insights"))
page_token = getPageToken(page, token)
getInsights(page, token=page_token, metric = "page_impressions")

~~However, on testing this again, I seem to be able to get the insights without any extra permissions, but I'm guessing that the token retained the scope somehow (?). Maybe you can test it as well. In any case, I think the two additions should probably not do any harm... ~~

Edit: it turns out app permissions are retained after asking a new access token without those specific permissions. You do need the read_insights and manage_pages permissions for some of the insights. However, I don't think you need a page_token, it just happened that I asked for the manage_pages permissions to get the page_token, but it was the permission that did the trick.

So, you can also decide to only accept the first commit, as the scope= argument is needed to run some insights, and it seems like a useful addition in any case. The getPageToken function might be useful but I don't see a direct use case. Do you want me to remove the second commit from the PR?

pablobarbera commented 7 years ago

This is great, thanks @vanatteveldt ! I decided to add both commits, as the getPageToken doesn't seem to create any issue, and perhaps someone finds it useful. To be honest, since I'm not an admin of a page, it's hard for me to test some of the code here, but from my basic tests everything seems correct. Thanks so much for your contribution!