Ionic with Backand social login: profile picture - facebook

I'm developing an application with ionic and Backand and I'm using the sign in with social accounts with the Backand service.
The social login process is OK, but I want to get the ID from Facebook and Google plus in order to get the profile picture.
I was talking with the support of Backand and I know that in Security and Auth there is an action "beforesocialSignUp" and I have not commented the line to get the data:
'use strict';
function backandCallback(userInput, dbRow, parameters, userProfile) {
// get facebook id and put in fuid field that you need to add to your user object
userInput.fuid = parameters.socialProfile.additionalValues.id;
// get gender from facebook and put in gender field that you need to add to your user object
// userInput.gender = parameters.socialProfile.additionalValues.gender;
}
And I have created the fuid field in my user object, but, when I make a login with Facebook or Google+ It doesn't save nothing. The login is OK but I can't get the ID data.
Any idea?

I had exactly the same problem.
The solution was to set the where condition of the BeforeSocialSignUp function from false to true.
This is nowhere in the documentation explained.
It took me 3 day ro figure this out :-(
Now the login works as expected and the fuid field gets assigned.
My next step is to get the profile picture from the FB Account, but therefore I have to make an API request to facebook with a user access token which I dont know how to get from backand.

Related

Facebook Matching API - Page Scoped ID (PSID) empty for other users

I have a bunch of users registred on my website thanks to Laravel Socialite and Facebook Login. When a user creates a account their app_scoped_id is save under provider_user_id.
I need to get the page_scoped_id of a user on my page instead of app_scoped_id.
To do so I am using the Facebook Matching ID whose documentation could be find here
The code
$fb = new Facebook(...);
$response = $fb->get($user->socialAccount->provider_user_id . '/ids_for_pages?fields=id&page=' . env('FB_PAGE_ID'), env('FACEBOOK_PAGE_TOKEN'));
$pageScopedId = $response->getDecodedBody()['data'][0]['id'];
Issue
It works perfectly for myself, but when I'm trying to get other users' page scoped id, it returns an empty array.
The Facebook Matching API is able to match page scoped id and app-scoped id if and only if they both exist.
In my case, page scoped id has not been associated with users (since they have not interacted with the messenger yet).

Generate access token Instagram API, without having to log in?

So I am building a restaurant app and one of the features I want is to allow a user of the app to see photos from a particular restaurant's Instagram account.
And I want a user to be able to see this without having to login to their Instagram account, so they shouldn't even need an Instagram account for this to work.
So I have read this answer How can I get a user's media from Instagram without authenticating as a user?
And I tried what it said and used the client_id(which I recieved when I registered my app using my personal Instagram account), but I still get an error back saying :
{
meta: {
error_type: "OAuthAccessTokenException",
code: 400,
error_message: "The access_token provided is invalid."
}
}
The endpoint I am trying to hit is :
https://api.instagram.com/v1/users/search?q=[USERNAME]&client_id=[CLIENT ID]
So do I absolutely need an access token for this to work(and thus have to enforce a user to log in) ?
If I do, then is there way to generate an access token somehow without forcing the user log in?
I believe there is a way around this, as the popular dating app Tinder has this desired functionality I am looking for, as it allows you to see photos from people's Instagram account without having to log in! (I have just verified this 5 minutes ago!)
Any help on this would be much appreciated.
Thanks.
Edit April 2018: After facebook privacy case this endpoint is immediately put out of service. It seems we need to parse the JSON embedded in <script> tag directly within the profile page:
<script type="text/javascript">window._sharedData = {"activity_counts":...
Any better ideas are welcome.
You can use the most recent link
GET https://www.instagram.com/{username}/?__a=1
to get latest 20 posts in JSON format. Hope you put this to good use.
edit: other ways aren't valid anymore:
https://www.instagram.com/{username}/media/
Instagram used to allow most API requests with just client_id and without access_token, the apps registered back in the day still work with way, thats how some apps are able to show instagram photos without user login.
Instagram has changes the API specification, so new apps will have to get access_token, older apps will have to change before June 2016.
One way you can work around this is by using access_token generated by your account to access photos. Login locally and get access_token, use this for all API calls, it should not change, unless u change password,if it expires, regenerate and update in your server.
Since the endpoints don't exist anymore I switched to a PHP library -
https://github.com/pgrimaud/instagram-user-feed
Installed this lib with composer:
composer require pgrimaud/instagram-user-feed "^4.0"
To get a feed object -
$cache = new Instagram\Storage\CacheManager();
$api = new Instagram\Api($cache);
$api->setUserName('myvetbox');
$feed = $api->getFeed();
Example of how to use that object -
foreach ($feed->medias as $key => $value) {
echo '<li><img src="'.$value->thumbnailSrc.'"></li>';
}

testing facebook real time updates / user comments

I'm trying to integrate realtime updates for user pages. I have a callback running on a given HOST url. Having this the subscription creation is:
G = facebook.GraphAPI()
app_token = config['FACEBOOK_APP_ID'] + '|' + config['FACEBOOK_SECRET']
path = config['FACEBOOK_APP_ID'] + '/subscriptions'
post_args = {'access_token' : app_token, 'callback_url' : HOST, 'fields' : 'feed', 'object' : 'page', 'verify_token' : 'token'}
G.request(path, post_args=post_args)
This seems to work just fine and a subscription is created. After this, for a given user I get oauth credentials with perms = ["publish_stream", "offline_access", "manage_pages", "publish_actions", "read_stream"]
Using this access token, I do a subscribe by adding the app to the page tabs:
G = facebook.GraphAPI(access_token)
path = "/%s/tabs" % page['id']
G.request(path, post_args={'app_id' : config['FACEBOOK_APP_ID']}
Now things work.... sort of. First thing is I'm not sure exactly how facebook testing should work in practice. Basically I created a bunch of test users using the app settings on developers page. Then using one of these users, I create two separate pages Page1, Page2 and I registered the app as a tab on these pages. Posts issued from the test users, or from the real user I created the app with are picked up. But I try to find one of the pages from some other real user (so not a test one and not the one I created the app with) and I cant. Even if I copy paste the entire URL, I just get a redirect to the first page. Are those pages only visible inside my own test app/users context ? Should I create a page from my real account and test with that? I'm just curious on how would I go around testing this in a real setup. Would I get ALL comments / posts on that page regardless of the user who does the posting or just posts from the Page / Admins of the page ?
Another separate problem I'm having is creating a comment from some user. So I'm using the exact same access token I've got above, and having a post from someone come it, I want to issue a reply in the form of a comment on that post. I have the facebook post id, so I just:
graph = facebook.GraphAPI(access_token)
graph.put_comment(facebook_post_id, message)
Say I issue this command using the access token I got from TestUser1 on a post that came on his own page Page1 . I would expect that the comment be posted on behalf of TestUser1 but instead it gets posted on behalf of Page1 . Is this expected as a side effect of the token I'm using? I've read here https://developers.facebook.com/docs/facebook-login/access-tokens/ that there are user access tokens and page access tokens; but I've tried the exact same thing with a token without the 'manage_pages' perms and still get the same thing.
Cheers, Bogdan

Access token error when trying to post a Facebook score in a canvas application

I'm trying to integrate the scoring system in my canvas app with Facebook's, implemented using MVC 3 and the 5.2.1.0 Facebook SDK.
A simplified hello-world variant of my game controller looks like:
public class MyController : Controller
{
[CanvasAuthorize("publish_action" /*And some others*/)]
public ActionResult Index()
{
var fb = new FacebookWebClient();
var scores = fb.Get("/me/scores"); // Works (I think)
fb.Post("/me/scores", new { score = 10 }); // Throws an exception
}
}
The call to get scores looks like it's giving me something sensible; the call to write a score value throws "(OAuthException) (#15) This method must be called with an app access_token."
What've I missed? The application id and secret are correctly set in my web.config - for example, I can successfully post an application apprequest in other parts of the actual application not shown in this stripped down test copy. Rummaging around with the debugger shows me that the FacebookWebClient object contains a non-empty access token field, and that that's included in the URI that fb.Post eventually uses.
The Facebook scores page (that Björn links to) mentions only publish_actions but I've tried including other pertinent sounding permissions, such as offline_access and user_games_activity to no effect.
I am assuming that the CanvasAuthorize attribute does the login correctly - it certainly seems to let me send an application apprequest, so it looks as if it's doing the right thing...
Your app needs the permission to write to the users profile. You can use the Graph API to request the required permissions from the user. If granted, Facebook will give you the required access token that you can then use in your request to Facebook. This practice ensures that you only perform actions, the user allowed you to.
Edit_: After looking at the docs: Are you sure you have the required permissions from the user like described here http://developers.facebook.com/docs/score/ ?
Please see this link. You'll need to get an facebook app. Using the apiId and SecretId, you can then post using the information in the link below. The key is adding &scope=manage_pages,offline_access,publish_stream to the url.
like so:
"https://graph.facebook.com/oauth/access_token?client_id={0}&client_secret={1}&grant_type=client_credentials&scope=manage_pages,offline_access,publish_stream";

Cannot get page "like" status using Facebook C# SDK

I am using the latest Facebook C# SDK (v5.0.40 at time of writing) from here: http://facebooksdk.codeplex.com/.
I have created a test iFrame app in Facebook and got the CSASPNETFacebookApp sample running so that it displays the name of the currently logged in user within Facebook.
What I would now like to do is display different content in my iFrame depending on whether the user has "liked" the page. However the signed_request never seems to contain that information. From what I can see in the FacebookSignedRequest.cs I will only get the payload which contains the page information if the algorithm is AES-256-CBC HMAC-SHA256 but I only ever get HMAC-SHA256 returned.
What am I doing wrong? How do I get it to return the page information? Is it a problem with the setup of my app in Facebook or a configuration issue with the .NET app?
Thanks in advance.
var fb = new FacebookClient("access_token");
dynamic parameters = new ExpandoObject();
parameters.method = "pages.isFan";
parameters.page_id = "{you page id}";
dynamic result = fb.Get(parameters);
bool isFan = result == true;
Neil Knight's answer is technically correct, you can use FQL to look up whether a user has liked a page and it did help to set me on the right path. However my issue was actually more one of set up rather than code. What I didn't understand is that you only receive the "like" information in the signed request if your app is running within the "context of a page". If you set it up correctly then Facebook will pass your app the like flag without the user needing to "Allow" your app.
The steps are:
(1) Create your iframe application in Facebook
(2) Set up a tab url for your app in the app settings. This is what the parent page will use when it generates a link in the left hand column to go to your app.
(3) Go to your "App profile page", the URL will be something like this: http://www.facebook.com/apps/application.php?id=12345 Where 12345 is your app ID.
In the left hand column below the logo image there should be a link "Add to Page". If you click on that link you will be presented with a list of pages that you are the admin for. Select the page you want to like in your app.
Now if you navigate to your page you should get a link to your app in the left hand column. It is only when clicking on that link that you will get the page id and like status passed through to your application.
Hope this helps someone having the same issue.
You could use FQL in order to achieve this. I have just done this using the following statement:
var the_query = FB.Data.query("SELECT uid FROM page_fan WHERE page_id = {0} and uid={1}", page_id, user_id);
In order for this to work, I had to ask the user to "Allow" my application so that I had permission to check to see if they liked the page. Then, it was a simple case of checking the result and displaying the necessary <div>:
the_query.wait(function (rows) {
if (rows.length == 1 && rows[0].uid == user_id) {
$("#myLikeContent").show();
} else {
$("#myNoLikeContent").show();
}
});