Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use facebook api v2.10 #128

Merged
merged 5 commits into from
Nov 20, 2017
Merged

Conversation

richard67
Copy link
Contributor

@richard67 richard67 commented Nov 4, 2017

Use current facebook api version v2.10 and adapt to changes in v2.9.

This also solves issue #127.

Beginning with v2.9, the api requires the fields parameter to be set if it shall return not only the
id but additional fields, and the fields have been reorganised and renamed.

See https://developers.facebook.com/docs/graph-api/changelog/version2.9#gapi-changes-general:

  • GET /{url}/share - The share endpoint has been removed and replaced with:
    • engagement field with subfields:
      • comment_count
      • comment_plugin_count
      • reaction_count
      • share_count

The Facebook Like button shows then the total of all these counts, except of the comment_plugin_count, which seems to belong to the deprecated comment plugin, so I did not include this one into the total.

See following examples from a test (the token parameter in the URL has to be replaced by a valid token you can get at the Facebook Api Explorer).

{
"share": {
"comment_count": 0,
"share_count": 674
},
"og_object": {
"id": "331776430263909",
"description": "News und Foren zu Computer, IT, Wissenschaft, Medien und Politik. Preisvergleich von Hardware und Software sowie Downloads bei Heise Medien.",
"title": "heise online",
"type": "website",
"updated_time": "2017-11-01T17:13:02+0000"
},
"id": "https://www.heise.de"
}

{
"engagement": {
"reaction_count": 153,
"comment_count": 107,
"share_count": 414,
"comment_plugin_count": 0
},
"og_object": {
"id": "331776430263909",
"description": "News und Foren zu Computer, IT, Wissenschaft, Medien und Politik. Preisvergleich von Hardware und Software sowie Downloads bei Heise Medien.",
"title": "heise online",
"type": "website",
"updated_time": "2017-11-01T17:13:02+0000"
},
"id": "https://www.heise.de"
}

How to test:

Verify that for an URL for which shariff-backend-php works well, the shariff-backend-php without this pull request applied and the and shariff-backend-php with this pull request applied show the same count for facebook if queried at the same time.

Why to change to latest api version:

When creating an app_id on the Facebook app dashboard, it is bound to the current API version, i.e. you can't select an api version lower than that. So if you today create an app, you can select only v2.10 as api version in the dashboard, and querying that app on Facebook Graph Object Explorer using api version v2.8 and a key made from the app_id and the secret of the app and not of a current user, you will not get a result.

Additional info:

When using a dedicated Facebook API version, e.g. "https://graph.facebook.com/v2.10/", and not only "https://graph.facebook.com/" without version, then the result is always in the format for that version, i.e. the check in the source code for array elements belonging to older API versions is not necessary. Therefore it is sufficient to check only for the result of that API version and not the older things "if (isset($data['data']) && isset($data['data'][0]) && isset($data['data'][0]['total_count']))" for very old versions which meanwhile are not available anymore and "if (isset($data['share']) && isset($data['share']['share_count']))" for version v2.8 in addition, like it is now in the source code, which checks both old alternatives. Maybe this was some remainder from old code when no version folder "/vX.Y" (like now "/v2.8") was used in the query URL, so the result could be returned in different formats depending on the version used by the APP belonging to the app_id parameter.

Use current facebook api version v2.10. Beginning with v2.9, the api
requires the fields parameter to be set if it shall return not only the
id but additional fields, and the fields have been reorganised and
renamed.
@richard67
Copy link
Contributor Author

Travis-ci failed but the reasons seem not to be related to my PR, or are they? Can some maintainer check?

@richard67
Copy link
Contributor Author

Ok, I saw for PHP 7.1 travis tells format errors. I will correct and commit in a minute.

Fix too long lines
@richard67
Copy link
Contributor Author

richard67 commented Nov 4, 2017

Now Travis succeeded for PHP 7.0. The others fail with errors which seem to be related to the test setup and not to this PR, but those don't show any code style errors anymore, so I assume this PR is OK now.

}
if (isset($data['share']) && isset($data['share']['share_count'])) {
return $data['share']['share_count'];
if (isset($data['engagement']) && isset($data['engagement']['reaction_count'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(isset($data['engagement']) is not needed when having all those other checks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was like that in the present code, too:
if (isset($data['share']) && isset($data['share']['share_count'])) { ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you know why travis fails?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://travis-ci.org/heiseonline/shariff-backend-php/jobs/297185279#L601

Failed to clone the git@github.com:heiseonline/shariff-backend-php.git repository

Seems to hit some github limitation. For sure not your fault.

it was like that in the present code, too

Does not mean that it was "correct" like that. ;-)
Simplifying code makes it better readable.

Simplify isset check and add some new lines for better readability
@richard67
Copy link
Contributor Author

richard67 commented Nov 5, 2017

@liayn Done, isset code corrected. Can you also have a look on my comment here?

I have meanwhile checked the other shariff backends.

For the Perl backend I could do a PR with the same changes as done here, but the Node backend still seems to use the meanwhile unsupported FQL, so the change there would be a bit bigger, and I am not sure if I can do that since not being an expert in JS.

Question: What shall I do regarding this? Make PR for other backends, too, whereever I can, or ignore them because soemone else will do that anyway sooner or later?

Another possible problem with Facebook's recent API versions is that if an object never has been scraped before by the FB crawler, a GET request to the open graph (OG) object containing the og_object in the list of fields to get will return an empy og_object. In this case Facebook say it needs a POST request to initialise the OG object for that particular API version. See https://developers.facebook.com/blog/post/2017/07/18/graph-api-v2.10/ , section "URL API Changes":

Website owners may update Open Graph objects but are limited in how frequently they may do so. Previously, GET /{url} would trigger a scrape if that URL had not been encountered before. With v2.10, these requests will not trigger a scrape nor an update to the Open Graph object. This means that API callers are less likely to be rate limited when they intentionally update Open Graph objects.

When making a GET request against a URL we haven't scraped before, we will also omit the og_object field. To trigger a scrape and populate the og_object, issue a POST /{url}?scrape=true. Once scraped, the og_object will remain cached and returned on all future read requests.

I have no idea if this is a problem for the counters, too, but if so, it could be a problem for shariff-backend-php when merging this PR here. A solution would be to change thos PR so it used API v2.9, which has the same format of counters but according to the cited text above does not have the changed behavior described above. Later then we could extend the backend so it can be used with v2.10 and later and will include the og_object in the fields list so it can check if that is empty and can issue the POST request. But if someone created his facebook app with version v2.10 or later, the counter on shariff backend will not work for him if it uses a lower version.

Question: Shall I change this PR to API version v2.9 to be on the safer side? Or should it be taken as it is?

}
if (isset($data['share']) && isset($data['share']['share_count'])) {
return $data['share']['share_count'];
if (isset($data['engagement']['reaction_count'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can combine this to isset($foo, $bar, $baz)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@core23 You are right. Done.

@richard67
Copy link
Contributor Author

@liayn Forget the 2nd part "Another possible problem" of my comment above. The counters will be zero for a new object, and first scraping by facebook crawler will then be issued after having clicked the button by Facebook share dialog.

@richard67
Copy link
Contributor Author

Travis fails seem not to be related to this PR. The logs do not show code style errors.

@liayn
Copy link
Contributor

liayn commented Nov 5, 2017

@richard67 I'm not a official maintainer nor do I use any of the other implementations. Hence, I can only say "I do not care too much".

@richard67
Copy link
Contributor Author

richard67 commented Nov 6, 2017

@compeak I just see there is an issue #127 which is solved by this PR. So it is not just an enhancement but a bug fix?

@compeak compeak added the bug label Nov 7, 2017
@compeak
Copy link
Collaborator

compeak commented Nov 7, 2017

Travis fails seem not to be related to this PR. The logs do not show code style errors.

There seems to be a problem with composer and GitHub. I am looking into this.

@ahoiroman
Copy link

Any idea, when this will be merged? My users (I created a little extension for Pagekit CMS) are waiting for this :)

@compeak
Copy link
Collaborator

compeak commented Nov 20, 2017

There seems to be a problem with composer and GitHub. I am looking into this.

Apparently Travis does not set "hidden" or encrypted environment variables anymore on builds originating from forked repositories. It looks like this causes problems with composer and GitHub API rate limits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants