How to get userID from access token in Facebook Graph API? [duplicate] - facebook-graph-api

I am developing an android app. I was using a HttpURLConnection to get profile pictures of facebook users. The URL looks like this:
http://graph.facebook.com/?ids=username1,username2,username3&fields=picture&type=square
But now, since v2.2 doesn't let me to get profile pictures of users who are not using my facebook app, it throws an error:
{ "error": {
"message": "(#803) Cannot query users by their username (username1)",
"type": "OAuthException",
"code": 803 } }
I am not interested in using "Facebook SDK for Android". Let's say I use a pop-up webview to authorize my facebook app. What url would let me do the same thing? Is there a simple solution like this:
graph.facebook.com/?app=APP-ID&ids=username1,username2&fields=picture

Since v2.0 of the API, you are not supposed to use usernames at all - and that´s why you can´t query users by their username anymore. The only way to get access to data of a user is by authorizing that user and using the /me endpoint.
Main rule: Forget about users who did not authorize your App, you are not supposed to get any data of those for privacy reason.

Related

How do I get instagram_basic permission during development?

I'm trying to use this endpoint from the Instagram graph api:
GET /ig_hashtag_search?user_id={user-id}&q={q}
Documentation for this endpoint can be found here.
I'm running this GET request:
https://graph.facebook.com/v14.0/ig_hashtag_search?q=coke&transport=cors&user_id=<my_user_id>&access_token=<my_access_token>&q=coke
and get this as response:
{
"error": {
"message": "(#200) Requires instagram_basic permission to manage the object",
"type": "OAuthException",
"code": 200,
}
}.
The user id comes from my instagram account and the access_token that I'm using is a User token generated with "Graph API explorer" and I can see that this token has the scope "instagram_basic" when using the "Access Token Debugger".
I can't understand why this does not work? Do I really need to submit a API review just to test the hashtag endpoint during development?
Please help!
I guess sometimes App review team of Facebook doesn't know what is the usage of the permission so they just decline the submissions randomly. I got that permission like 3 months ago and i checked my app review. I send them this text
We are going to use instagram_basic to read an Instagram account profile's info and media. We will get the basic metadata of an Instagram Business account profile like "USERNAME", "ID", "FOLLOWERS", "FOLLOWING". You can check the Screescast to see how we use the instagram_basic on our app.
Here is what they saw while they do the app review
Also be careful while you send requests, keep in mind to change it to development mode. Some of the endpoints works only in live mode. The API still changes a lot and there are tons of bugs i see in my project. I hope this helps.

How to like on behalf of the user - Facebook Workplace API

While working with the Facebook Workplace API liking an object (post, comment, photo..etc) can be done by passing the Admin Access Token, I'm trying to figure out how to do it on behalf on the user for a feed / comment posted into a group.
The docs mention getting the Page Access Token to perform this action but a Group doesn't have an Access Token & doesn't really solve what I'm going for anyway.
I've tried getting the User Impersonation Token from the API using the Admin Token but posting to the likes graph endpoint results in a publishing error.
POST /https://graph.facebook.com/{COMMENT-ID or POST-ID}/likes
HEADERS: Authorization Bearer "{IMPERSONATION-TOKEN}"
Error Response:
{
"error": {
"message": "(#3) Publishing likes through the API is only available for page access tokens",
"type": "OAuthException",
"code": 3,
"fbtrace_id": "CGpXtFWrwiA"
}
}
Again this works fine using the Admin App Access Token but not on behalf of the user. Also a group doesn't have an access token unlike a regular FB Page.
Any help on this would be great!

Facebook Graph API, v3.2 - How to get user media for an Instagram account (3.6.2019)?

I am working on Instagram API integration to our service. As of 3.6.2019, I have tried to get media for a particular Instagram user account. I have tried to understand the documentation, and this is what I have working so far:
I have an Instagram business account
I did attach my Instagram account to my business page by logging in
I have obtained a page and a user access token via Graph API Explorer. User token has the following permissions:
email
instagram_basic
manage_pages
pages_show_list
pages_messaging
public_profile
I am able to get information about the associated Instagram account like profile_pic and username.
What perplexes me about this is that when I read the documentation, user info retrieval seems to work alright:
However, if I changed the URL to match the media retrieval URL and keeping the same user ID, I get the following error
even through the documentation says:
To get all media objects on an Instagram Business User, send a GET request to the User's /media edge.
And the sample request URL provided:
GET graph.facebook.com/17841405822304914/media
Source: https://developers.facebook.com/docs/instagram-api/reference/user/media/
What am I doing wrong? The URL seems to follow the documentation description. I did use the user access token for the media request. The access token contains permission for instagram_basic, as needed based on the documentation:
Permissions
An access token from the Instagram Business User, with the following
permissions:
instagram_basic
I am using API version 3.2.
Do you really use the right instagram id? Try calling this API with the ID you used:
https://graph.facebook.com/v5.0/[ENTER_ID]?fields=instagram_business_account{id, username,profile_picture_url}&access_token={}
I you get a result like this:
{
"instagram_business_account": {
"id": "XXXXXXX",
"username": "XXXX",
"profile_picture_url": "https://scontent.xx.fbcdn.net/v/t51.2885-15/44644278_733652060347074_11957"
},
"id": "XXXXXXX"
}
You know, that you used the wrong ID :D
Go ahead and call the /media endpoint with the ID nested under instagram_business_account

How to post to a facebook page feed from external web site

We have a company-owned web application that should post to our company-owned facebook page.
should be a server-to-server call
the user in our webapplication who makes the post in the webapplication gui, should ideally not have to be a facebook admin for the facebook page.
We have a facebook App that is added to a testpage on facebook, and when I do this API call:
https://graph.facebook.com/PAGE-ID/feed?message=MESSAGE-CONTENT&access_token=APP-ID|APP-SECRET
..the answer I get back is:
{
"error": {
"message": "(#200) The user hasn't authorized the application to perform this action",
"type": "OAuthException",
"code": 200,
"fbtrace_id": "TRACE-ID"
}
}
It seems like I need the publish_pages right, and this right require an App-review (unless users are known and assigned roles in the app), but it says in the review info from facebook, that automatically publishing is not allowed for this right.
We can save a token in database and reuse when posting to Facebook Graph API.
Is this impossible without a facebook user doing a Facebook login in our webapplication?
Should we instead get a long-lived user token from a facebook-page-administrator, and then reuse this when posting, and re-login to facebook client side, before token expire?

why does posting to facebook page yield "user hasn't authorized the application"

I have read the fb docs and written code to publish a message to a facebook "page", however I am getting an error that I don't expect to see:
(#200) The user hasn't authorized the application to perform this action
Here's what I've done:
I set up a facebook application, which provides my APP_ID and
APP_SECRET.
I set up a test facebook "page". Let us refer to its fb id as PAGE_ID.
Used OAuth to get a USER_ACCESS_TOKEN with scope "publish_actions,manage_pages" for the user. I accepted the permissions requested by my app when redirected to the facebook auth page.
I did a GET on https://graph.facebook.com/me/accounts using the USER_ACCESS_TOKEN, and I get back a list of pages I administrate, including the one I want to post to.
This block of data for my page looks like:
{
"data": [
{
"category": "Community",
"name": "My Generic Test Page",
"access_token": PAGE_ACCESS_TOKEN,
"id": PAGE_ID,
"perms": [
"ADMINISTER",
"EDIT_PROFILE",
"CREATE_CONTENT",
"MODERATE_CONTENT",
"CREATE_ADS",
"BASIC_ADMIN"
]
},
....
]
}
Then I use the PAGE_ACCESS_TOKEN to post a message to the page:
I did a POST on https://graph.facebook.com/PAGE_ID/feed with a field message equal to This is a test post.
Facebook returns:
{
"error": {
"message": "(#200) The user hasn't authorized the application to perform this action",
"type": "OAuthException",
"code": 200
}
}
Using the token debugger, I can confirm that my PAGE_ACCESS_TOKEN is valid, and has scopes: manage_pages and publish_actions.
Where am I missing authorizing the application? Do I need additional scopes? Did I miss clicking something on the facebook authorization screen? Is there a setting on the app I am missing? After days of debugging this, I must be blind to the problem. :-|
You should add permission called status_update, for example
https://www.facebook.com/dialog/permissions.request?_path=permissions.request&app_id=145634995501895&redirect_uri=https%3A%2F%2Fwww.facebook.com%2Fconnect%2Flogin_success.html%3Fdisplay%3Dpage&response_type=token&fbconnect=1&perms=status_update&from_login=1&m_sess=1&rcount=1
and i'm able post to page i liked with the access token i get just now:
If you want to post as the admin of the page, you're require both manage_pages and status_update permissions, for example
https://www.facebook.com/dialog/permissions.request?_path=permissions.request&app_id=145634995501895&redirect_uri=https%3A%2F%2Fwww.facebook.com%2Fconnect%2Flogin_success.html%3Fdisplay%3Dpage&response_type=token&fbconnect=1&perms=manage_pages%2Cstatus_update&from_login=1&m_sess=1&rcount=1
Cheers
status_update is not used anymore. To publish on pages, I had to use both manage_pages and publish_pages.
Well, this seems to be a common mistake that most of us make while trying to do an activity in social netwroks. Before trying to put up an open graph action,You need to set the permissions in your initial authorization request . By default you only gain 'read-only' access to their basic information.
Settintg up permisson at teh time of authetication is a must for Facebook and LinkedIn APIs..
See the public_actions section in Facebook open graph permissions here and make relevant changes in the authorization code , and get your issue solved.
I found the best way to get a valid token and check permissions was via the Graph API Explorer BUT while Facebook's documentation is extensive it is not always the easiest to follow.
In the explorer you have to look at both:
The Application Currently at the top and quite subtle, I missed this for ages.
Get Token Dropdown What you click to get a Token, when you click the arrow you can choose pages and other items you have access to for selecting a token for.