3

I am writing a simple procedure that automatically makes a facebook post. From what I understand, I need to have a "user access token" to do this. I am using Koala (but the philosophy is similar for other libraries). Anyway, I create a new OAuth account:

@oauth = Koala::Facebook::OAuth.new(app_id, app_secret, callback_url)

The koala instructions then become somewhat unclear. The next two lines are:

@oauth.url_for_oauth_code # generate authenticating URL
@oauth.get_access_token(code) # fetch the access token once you have the code

Where does the "code" variable come from? It doesn't say in the documentation. Also, does the "get_access_token" method get an "app access token" or a "user_access_token"? The method name is not clear. I tried going to the url that the [url_for_oauth_code] method gave me, but it gives me no code! Where does the "code" variable come from?

Steve Quezadas
  • 724
  • 2
  • 11
  • 27
  • Did u find the answer? – poramo Jul 16 '15 at 16:33
  • Hi everyone. I am very much novice to the use of API. In `@oauth = Koala::Facebook::OAuth.new(app_id, app_secret, callback_url)` what should be the value of **callback_url**? I want to refresh the token of existing logged in user. Thank you in advance. – Vishal Jan 26 '17 at 04:30

1 Answers1

4

On the front page of Koala it states you need to go through the OAuth process described at http://developers.facebook.com/docs/authentication/ (this is an old link but the content within is valid)

Specifically

@oauth.url_for_oauth_code

https://github.com/arsduo/koala/blob/master/lib/koala/oauth.rb#L85 Generates a URL that you need to direct the user to based on the repo it's something like

https://www.facebook.com/dialog/oauth?
    client_id={app-id}&
    redirect_uri={redirect-uri}&
    scope=email

Based on the documentation https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow/v2.2#login, when the response_type is omitted the default response type is code. So the above is equivalent to

https://www.facebook.com/dialog/oauth?
    client_id={app-id}&
    response_type=code&
    redirect_uri={redirect-uri}&
    scope=email

So on redirect to redirect-uri, this URL will be appended with the code param which you must handle then supply to

@oauth.get_access_token(code)

The access token is a user access token.

phwd
  • 19,975
  • 5
  • 50
  • 78
  • Ok, i am still confused. When you say "Generates a URL that you need to direct the user to based on the repo it's something like" . When you say "based on the repo", what do you mean "repo"? Is this short for "repository"? Like a github repository? I saw the redirected URL had a long string which I assume is the [code] variable. I got this error: Koala::Facebook::OAuthTokenRequestError Exception: type: Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request. – Steve Quezadas Jan 08 '15 at 18:44
  • Maybe I am using the wrong type of token. I just want a program to automatically make a posting on a facebook feed from time to time (an automated "announcement"). Is it better to use a "page access token" instead? – Steve Quezadas Jan 08 '15 at 18:47
  • @SteveQuezadas I took the time to browse the github repository for koala which has the source outlining what each function (url_for_oauth_code,get_access_token) does. Your redirect_uri is not correctly configured http://stackoverflow.com/questions/16345777/given-url-is-not-allowed-by-the-application-configuration go to your app settings and ensure they are matching. – phwd Jan 08 '15 at 18:48
  • @SteveQuezadas you cannot automatically get a user token. To get a page token you need to get a user token. – phwd Jan 08 '15 at 18:49
  • Yes, you are correct, the URL was slightly off (didn't ahve a trailing "/" at the end. Thank you very much for your help! – Steve Quezadas Jan 08 '15 at 19:13