Facebook SDK android, get connected accounts - android

A facebook user can control multiple pages. For example, a user can have their personal facebook account, their public figure page and their business page. This shows up as three distinct accounts with their own User information in facebook's server.
I want to list these things, how can I retrieve what these accounts are using the Facebook SDK?
I feel like it has to do with the Request.newMeRequest method, but this returns a GraphUser object.
and the Request.newMyFriendsRequest returns a list of users, but not a list of the personal account's connected accounts.
edit I am now using this
new com.facebook.Request(
session,
"/me/accounts",
null,
HttpMethod.GET,
new com.facebook.Request.Callback() {
#Override
public void onCompleted(com.facebook.Response response) {
if(response!=null && response.getGraphObjectList()!=null){
}
}
}
).executeAsync();
but response is null. My session has the manage_pages permission requested as well
edit I tried replacing "/me/accounts" with "/"+userId+"/accounts" and I still get back a null response. Baffling.
Insight appreciated

You can test this procedure at Graph Api Explorer -> Just click on 'Get Access Token' button-> under 'Extended permission' check 'manage_pages' & submit it. It will give you the admin-page-details JSON.
And please see Graph API Reference /{user-id}/accounts

Related

Sessions with new facebook sdk android

I want to launch a new activity after successfully logging in using facebook login and maintain my session across all activities. I also don't want the log out in the next screen.
Samples for the new sdk don't provide any info about sessions so I am not sure how to go about this. Do we use AccessToken or Session class to maintain session and could anyone please provide links or examples for the same?
Once you initialize the Facebook SDK in your app and go through the login steps and get the user access token, your application will be logged in until you logout from user account via code.
And you can fetch the access token and check its status and validity (once the user logged in via your app) from any of your application activities via:
mAccessToken =AccessToken.getCurrentAccessToken(); // Which is a static function.
if(mAccessToken == null) // user are not logged in
{
// Proceed with your log in logic / code.
}

Accessing Facebook Graph API without login

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

Facebook Friends Request - Missing Friends

I am requesting to get user friends from an Android App that I am developing. As from Facebook Api V2.0 I know that I should get only user friends that have already logged in through my App. However, although I know certain friends of a user have logged In through my App they do not appear in Facebook Request Response when requesting friends of that user. For example I get back 40 friends rather than 50+.
Has anyone experienced this behavior before? I already deleted app from few users to re-authorized it through login but I haven't see any change in the behavior.
Here is the code I'm using:
new Request(ParseFacebookUtils.getSession(),"me/friends", null, null, new Request.Callback(){
#Override
public void onCompleted(Response response) {
if (response == null){
return;
}
else if (response.getError() != null){
response.getError().getException().printStackTrace();
return;
}
GraphMultiResult result = response.getGraphObjectAs(GraphMultiResult.class);
List<GraphObject> fbInList = result.getData();
if (fbInList != null && !fbInList.isEmpty()){
for (GraphObject user : fbInList) {
JSONObject jsonUser = user.getInnerJSONObject(); // The Facebook User
System.out.println("name: " + jsonUser.optString("name"));
}
}
}
}).executeAsync();
I've found what was the problem. When I updated to the latest Facebook SDK, Facebook was returning only 25 friends. I needed to use paging or add a limit to my Friend Request. On previous SDK I didn't need to.
Adding limit:
Bundle params = new Bundle();
params.putString("limit", "50"); // Up to 5000?
friendRequest.setParameters(params);
Relevant Stack Overflow Questions:
Facebook graph API 'friends' request now only returning 25 friends per page? What's going on?
newMyFriendsRequest Facebook returns only 25 friends
Useful Facebook link for paging:
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.2#paging
The relevant portion of the doc for you is this
In v2.0 of the API I'm unable to get the full friend list of someone who has logged into my app - is there a way to get the full friend
list?
With Graph API v2.0 and above, calls to /me/friends return only the
friends who also use your app. These friends must have also granted
the user_friends permission. In the cases where you want to let people
tag their friends in stories published by your app, you can use the
Taggable Friends API. If you want to invite people to your app, we
have a number of solutions that depend on the type of app you've built
and the platforms you've built for. Please see our question about
Inviting Friends for more information.
So along with only friends using your app, they should also have authorized user_friends otherwise you wont get their details. Have you done this?

Create Facebook access token without logging in

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.

Retrieve user country through Facebook Android Graph API

I'm developing an Android application where the user can use his facebook credentials and I list some of his informations like the location. However, I noticed the format of location is something like "location:{id:335457, name: City, State}". I'd like to know if there's a way to also retrieve the user country. I saw the Facebook FQL could help, but, on current SDK version, it is deprecated. My call to the API method:
new Request(session, params, null, HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
// treats the response
}
}).executeAsync();
I also looked for something, but without success in my case. I'm using the latest Facebook Android SDK available.
Thank you in advance for the help.
The location field of the user is set manually and does not neccessarily mean that he's currently in that location. It's meant to be the current living location.
See
https://developers.facebook.com/docs/graph-api/reference/v2.2/user/#fields
The call would be
/me?fields=id,location
What's returned is a page_id, for which's location you can query for with
/{page_id}?fields=id,location
The location object contains a country field.
See
https://developers.facebook.com/docs/graph-api/reference/v2.2/page

Categories

Resources