trying to get facebook insight data

745 views
Skip to first unread message

tim

unread,
Apr 18, 2011, 11:49:21 PM4/18/11
to rest-graph
I am trying to get facebook insight data and I am hitting a brick
wall. Basically I did this.

1. I set up a facebook app and got the app_id, API_key etc.

Then I try to log in like this but it doesn't work.

rg = RestGraph.new(:app_id => @app.app_id ,:secret =>
@app.app_secret )
rg.authorize!(:redirect_uri => 'http://w3.org/', :code => 'CODE')

it complains about the redirect URL. The problem is that I don't want
a redirect URL. I want an access token without any manual intervention
at all.

If I get an access token by other means I still can't pull up the
insight data. It doesn't matter if I use FQL or Graph I get an empty
array []

I suspect this is beccause I am using my app key to log in but trying
to get insight data about my facebook page.

So is this even possible?

Lin Jen-Shin (godfat)

unread,
Apr 19, 2011, 4:07:15 AM4/19/11
to rest-graph, Lin Jen-Shin (godfat)
On Apr 19, 11:49 am, tim <timuc...@gmail.com> wrote:
> I am trying to get facebook insight data and I am hitting a brick
> wall. Basically I did this.
>
> 1. I set up a facebook app and got the app_id, API_key etc.

FYI: api_key is no longer used in Graph API.
It's app_id and secret needed.

> Then I try to log in like this but it doesn't work.
>
> rg = RestGraph.new(:app_id => @app.app_id ,:secret =>
> @app.app_secret )
> rg.authorize!(:redirect_uri => 'http://w3.org/', :code => 'CODE')
>
> it complains about the redirect URL. The problem is that I don't want
> a redirect URL. I want an access token without any manual intervention
> at all.

A redirect is a must because that's the flow to get
authorization and authentication. The flow is,
first redirect the user to Facebook, and then user
login to Facebook and click on "Allow" (authorization),
then Facebook would redirect the user back to your
application, and you'll get the code to exchange the
access token.

This redirect_uri is where you want Facebook redirects
back to your application. This can't be omitted and is
a must, otherwise Facebook won't know where it should
direct the user to go back.

> If I get an access token by other means I still can't pull up the
> insight data. It doesn't matter if I use FQL or Graph I get an empty
> array []
>
> I suspect this is beccause I am using my app key to log in but trying
> to get insight data about my facebook page.
>
> So is this even possible?

Regarding the insight data, if you mean this one:

https://developers.facebook.com/docs/reference/api/insights/

Then I think instead using a user's access token,
you might need to use the so called "secret_access_token"
instead. I don't know where did Facebook document this,
but say your application id is 123, then you might want to
do this in rest-graph:

RestGraph.new.get("123/insights", {}, :secret => true)

This way, rest-graph would try to use secret_access_token
instead of the normal access token. You can take a look at
your secret access token by calling RestGraph#secret_access_token.
The definition is:

def secret_access_token
"#{app_id}|#{secret}"
end

Many Facebook API needs this access token instead the
normal one. I think where data you want to fetch is not
related to any user but your applications, you'll need this.

Tim Uckun

unread,
Apr 19, 2011, 4:41:43 AM4/19/11
to rest-...@googlegroups.com
>
> Then I think instead using a user's access token,
> you might need to use the so called "secret_access_token"
> instead. I don't know where did Facebook document this,
> but say your application id is 123, then you might want to
> do this in rest-graph:
>
>    RestGraph.new.get("123/insights", {}, :secret => true)
>
> This way, rest-graph would try to use secret_access_token
> instead of the normal access token. You can take a look at
> your secret access token by calling RestGraph#secret_access_token.
> The definition is:
>
>    def secret_access_token
>      "#{app_id}|#{secret}"
>    end
>
> Many Facebook API needs this access token instead the
> normal one. I think where data you want to fetch is not
> related to any user but your applications, you'll need this.


I found a web site that says this

Obtaining the Authorization Code

To use the Graph API, you must obtain an access token with the
read_insights permission. There is an important authentication
difference between application insights and insights from pages or
external websites:

You can use either a user access token (on behalf of the application
owner) or an application access token (on behalf of the application
itself) to get insights about an application.
You can only use a user access token (on behalf of the page owner) to
get insights about a page or an external website.

As far as I can tell the method you outlined using app_id and secret
is to get an application access token but that won't give you
permission to access the insights. In order to get the insights I need
a user access token and it seems like you are saying there is no way
to get that without physical user involvement.

Am I reading that right? Surely there has to be a way to get that data
without having a user actually interact with a browser.

Lin Jen-Shin (godfat)

unread,
Apr 19, 2011, 5:05:52 AM4/19/11
to rest-...@googlegroups.com, Lin Jen-Shin (godfat)
On Tue, Apr 19, 2011 at 4:41 PM, Tim Uckun <timu...@gmail.com> wrote:
> I found a web site that says this
>
> Obtaining the Authorization Code
>
> To use the Graph API, you must obtain an access token with the
> read_insights permission. There is an important authentication
> difference between application insights and insights from pages or
> external websites:
>
> You can use either a user access token (on behalf of the application
> owner) or an application access token (on behalf of the application
> itself) to get insights about an application.
> You can only use a user access token (on behalf of the page owner) to
> get insights about a page or an external website.
>
> As far as I can tell the method you outlined using app_id and secret
> is to get an application access token but that won't give you
> permission to access the insights. In order to get the insights I need
> a user access token and it seems like you are saying there is no way
> to get that without physical user involvement.
>
> Am I reading that right? Surely there has to be a way to get that data
> without having a user actually interact with a browser.
>

Actually, sorry that I don't really know the detail. Since the documentation
says so, I guess using user access tokens with read_insights permission
would be able to read the insights.

But sorry I am too lazy to try it. Facebook has too many weird places
where document is wrong or not documented. For example, the term
"like" has so many meanings in different places, and they are not the
same, can't be read with this API or that API...

At least, the method I mentioned above worked for me, here's some
result I just tried:

>> response = rg.get("#{rg.app_id}/insights", {}, {:secret => true})

=> {"data"=>[{"name"=>"application_block_adds_unique",
"period"=>"day",
"id"=>"233082465238/insights/application_block_adds_unique/day",
"values"=>[{"end_time"=>"2011-04-15T07:00:00+0000", "value"=>6},
{"end_time"=>"2011-04-16T07:00:00+0000", "value"=>0},
{"end_time"=>"2011-04-17T07:00:00+0000", "value"=>7}],
"description"=>"Daily Blocks of your app (Unique Users)"}, ...
[snipped]

>> response['data'].size
=> 181

I don't know if this is the full result though. According to the permission
on the access token, Facebook would give you different results.
Maybe using read_insights permission from some user would give
you more data, or the data specific to that user, I don't know.

If this is true, to workaround a user interaction, you could try to grant
the user access token with read_insights along with offline_access
permission, and put that persistent access_token directly inside your
program. This way, as long as the user didn't change the permission,
it should work until Facebook changed something again.

Hope this is helpful, and you can get fetching your data soon.

Tim Uckun

unread,
Apr 19, 2011, 5:25:30 AM4/19/11
to rest-...@googlegroups.com, Lin Jen-Shin (godfat)
> Actually, sorry that I don't really know the detail. Since the documentation
> says so, I guess using user access tokens with read_insights permission
> would be able to read the insights.
>
> But sorry I am too lazy to try it. Facebook has too many weird places
> where document is wrong or not documented. For example, the term
> "like" has so many meanings in different places, and they are not the
> same, can't be read with this API or that API...
>
> At least, the method I mentioned above worked for me, here's some
> result I just tried:
>
>>> response = rg.get("#{rg.app_id}/insights", {}, {:secret => true})

I was able to get insights for the app I defined using the app_id but
not the page using the page object it but I'll try it again using your
method and see what happens.


>
> If this is true, to workaround a user interaction, you could try to grant
> the user access token with read_insights along with offline_access
> permission, and put that persistent access_token directly inside your
> program. This way, as long as the user didn't change the permission,
> it should work until Facebook changed something again.
>

If the access token does not expire I could to this I think.

> Hope this is helpful, and you can get fetching your data soon.
>

Thanks. I will try your method above.

Reply all
Reply to author
Forward
0 new messages