I am working in an android application where I use the facebook android sdk to login to the application.I get the access token from the facebook sdk when user logged in to the application through facebook sdk.
So is it possible to like a page in facebook pragmatically having access token of a user.
It's not possible to like a page programmatically, only comments and posts. I don't know why you marked the previous answer as correct, it does not work and it's quite misleading.
To like a post on facebook, I use this part of code to create the request.
Request likeRequest = new Request(Session.getActiveSession(), pageid + "/likes", null, HttpMethod.POST, new Request.Callback() {
#Override
public void onCompleted(Response response) {
}
});
Where I already logged in to Facebook, so I can just call the Session.getActiveSession().
Then I execute the request with:
Request.executeBatchAndWait(likeRequest);
You should run this in an Asynctask, since you are not allowed to do network stuff on your main thread.
Related
I've seen that with Javascript API and via GET requests, it is possible to get public posts from FanPages.
What i'm trying to acomplish is an Android Native App that doesn't ask user to get logged and with the FB App access token gets and shows the posts from the FB Page.
I'm wondering if this is possible via Android FB API as I can accomplish this via urls like
https://graph.facebook.com/{page_id}/feed?fields=message,picture&limit=10&access_token={your_acces_token}
When i'm trying this with Android Graph API, without user loging,
AccessToken.getCurrentAccessToken()
returns null
Thank you
I was struggling with the same for a while and after hours of research I came up with this (source):
#Override
protected void onCreate(Bundle savedInstanceState) {
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(getApplication());
AccessToken at = new AccessToken(getString(R.string.facebook_access_token), getString(R.string.facebook_app_id), getString(R.string.facebook_page_id), null, null, null, null, null);
Bundle parameters = new Bundle();
parameters.putString("fields", "message,created_time,link,full_picture");
new GraphRequest(at, "/" + at.getUserId() + "/feed", parameters, HttpMethod.GET, new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
Log.i(this.getClass().getName(), "onCompleted " + response.getRawResponse());
}
}).executeAsync();
}
I assume you already have a page access token. I created mine with the graph explorer from facebook. Here is a tutorial on how to create a never expiring one:
How to get a never expiring Facebook Page Access Token (look for the 2016 Method)
As far as I know, the extended token does not expire as long as it is used. But I am not sure about that. I think I red this somewhere in the facebook api docs but cannot find it anymore.
There are a lot other parameters you can set. See here Page and look for 'Fields'
Hope this clumsy solution is helpfull to someone.
There are two ways to do this and they are both documented here.
The simpler method is to simply concatenate your AppID and AppSecret (found in your Facebook developer dashboard) with a "|" character like so: 'App ID' + '|' + 'App Secret'
BE WARNED!
This will expose your App's Private Credentials in Client Side Code if someone decides to look carefully. Therefore it is much safer to setup a private backend server to act as a proxy for your mobile application's user-less access to the graph API.
You could set up an API in any language of your choice with a single public route that simply copies the request it receives, appends the credentials, sends the request to Facebook, and pipes Facebook's response back to the original requestor.
For Example:
Without Access Token:
Mobile App ---> http://www.YourServerDomain/{page_id}/feed?fields=message,picture&limit=10
With Access Token:
Your Server ---> http://graph.facebook.com/{page_id}/feed?fields=message,picture&limit=10&access_token=AppID|AppSecret
Response Pipeline
Facebook ---> API RESULT ---> Your Server ---> API RESULT ---> Mobile App
I am creating an Android app. I integrated Facebook SDK version 4.2.0. I successfully completed Facebook login and I got the Profile Details (Profile Picture,Profile Name etc).
My requirement is that I need to bring all the news feeds from the user's account to my app. Is there any way to achieve this? I searched a lot for this.. but nothing turned up that fitted my requirement.
The graph API should do the trick!
https://developers.facebook.com/docs/graph-api/
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
}
}
).executeAsync();
This way you can directly use it with the facebook sdk
https://developers.facebook.com/tools/explorer?method=GET&path=me%3Ffields%3Did%2Cname& with this link you can try your requests
saw that if you ask for example the posts you need a specific access token!
I want to receive the public posts of a defined facebook page in my app. I already integrated the FacebookSDK and created a new app in the Facebook developer console. My Request looks like this:
new Request(
null,
"/176063032413299/feed",
null,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
tv.setText(response.getRawResponse());
}
}
).executeAsync();
The server answers, that I need an acceess token. But I don't want my users to be logged in, when they use my app. So is there an option to get an access token without being logged in? I only want to read public data. I am not interested in publishing or reading private posts.
If I use the app access token like this, it still does not work:
new Request(
null,
"/176063032413299/feed?access_token=xxx",
null,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
tv.setText(response.getRawResponse());
}
}
).executeAsync();
I get the response:
"Invalid OAuth access token signature"
Even for public posts, you have to authorize the user in order to get access. Either with read_stream to get ALL posts or with user_status to get the posts of the user only.
read_stream will most likely not get approved by Facebook though, see this document: https://developers.facebook.com/docs/facebook-login/permissions/v2.2
Keep in mind that "public" does not mean you can grab it without user authorization. Apps can´t just scrape what they want - scraping is not allowed anyway: https://www.facebook.com/apps/site_scraping_tos_terms.php
Also, of course you can´t create a User Token (which is what you need) without user interaction (login and authorization). Detailed information about Access Tokens can be found in the following links:
https://developers.facebook.com/docs/facebook-login/access-tokens/
http://www.devils-heaven.com/facebook-access-tokens/
Btw, the docs mention that "Any valid access token is required to view public links." - So you may be able to get links only.
Source: https://developers.facebook.com/docs/graph-api/reference/v2.2/user/feed
For debugging Access Tokens, use the Facebook Debugger: https://developers.facebook.com/tools/debug/
Edit: I just realized that you just want to grab the public feed of a Facebook Page, not a User Profile. For that, you can just use an App Access Token. It´s never expiring and easy to create: App-ID|App-Secret. Check out the docs for more information. Keep in mind that you would need to use a User Token or Page Token if the Page is restricted by age or country.
Have a look at https://developers.facebook.com/docs/graph-api/reference/v2.2/page/feed/
The permissions needed to be able to request /{page_id}/feed:
An access token is required to view publicly shared posts.
A user access token is required to retrieve posts visible to that person.
A page access token is required to retrieve any other posts.
Meaning you could generate a long-living Page Access Token to be able to retrieve the public Page Posts. As you won't want to store an expiring Access Token in your App, you'll have to create a backend web service to proxy the requests between the Graph API and your app.
See
https://developers.facebook.com/docs/facebook-login/access-tokens/#pagetokens
https://developers.facebook.com/docs/facebook-login/access-tokens/#extendingpagetokens
https://developers.facebook.com/docs/facebook-login/access-tokens/#refreshtokens
You should also be able to use an App Access Token, which is valid indefinitely.
Use client token.
You could also generate and sign your own personal token but you don't want to distribute that.
I'm trying to use the facebook Graph API in my android application to retrieve the last posts from a Facebook Page. I have everything set up, so I can login with any account and even make a "/me" request to get the user's basic info. But when I try to make this request:
new Request(Session.getActiveSession(),
"PokerNewsBrazil?fields=posts&fref=ts/posts",
null,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
/* handle the result */
}
}
).executeAsync();
I receive the following error: "An access token is required to request this resource".
But my access token exists and it is valid, because if I use the access token from the same session in the Graph API Explorer I receive a succesfull response containing the posts from that page.
Where am I getting it wrong?
Change HttpMethod.GET to HttpMethod.POST.
This error arises because the SDK posts the access token behind the scenes, while the API is looking for it in a GET parameter.
For some reason using HttpMethod.GET and appending the access token to the url string causes various malformed access token errors.
I also encountered various errors when batching requests. If you encounter such errors, try executing a request standalone and debug from there.
The Facebook SDK is iterating relatively quickly yet the documentation is lacking and not up to date. Stick with it.
I want to implement "Like" option in my Android app, but I don't know which Request to use.
I have a valid facebook Session opened and the ID post I want to like.
How can I implement this function?
Thanks
I found a solution.
To like post I've implemented a simple POST request using likes connection of facebook post id.
This is the code:
Request likeRequest = new Request(Session.getActiveSession(), fBPostId + "/likes", null, HttpMethod.POST, new Request.Callback() {
#Override
public void onCompleted(Response response) {
Log.i(TAG, response.toString());
}
});
Request.executeBatchAndWait(likeRequest);
A cursory search (1, 2, 3) seems to indicate that it's not possible to directly "Like" things as a user via the Graph API, for security/spam reasons. One commonly-suggested alternative has traditionally been to show a Facebook-controlled "Like" button in a WebView and include that in your application instead.
However, more recently, the Facebook developer guide has provided one possible solution for how you can implement "like"-style functionality in your app when it comes to publishing stories on the user's behalf, though it's not precisely the same thing as what you seem to be asking for.