Ameba Arduino: [RTL8195AM] Let Ameba post articles on Facebook wall

DEPRECATED: Facebook APP posts articles on behalf of users, will no longer be available after Facebook GRAPH API v3.0 (2018/05/01) publication

Facebook is an online social networking website for users to communicate, to share, and to participate in groups. And it provides Graph API for developers to develop applications on different platforms, includes iOS, Android, web application and Facebook Canvas. Applications can make use of users’ resources and services under users’ agreement.

Graph API is based on HTTP protocol API, and this example will use Graph API to let Ameba post articles on the wall of Facebook.

Preparation

  • Ameba x 1

Example

  • Set up Facebook application

In order to let Ameba access Facebook, we need to set up the application first. The reason is that Facebook needs to know the identity of the users and the applications that require accessing it. Go to the website: https://developers.facebook.com/apps
It will show up the option to be the developers if it is the first time to use it. Then, click to sign up.

2

Show up a menu and to fill in the job tile and the Facebook agreement policy. Then click Register.

2

After success, click “Done”

2

If you have not developed any Facebook application before, it will appear the following menu to choose. We choose “Website” in this example.

2

Click the “Skip and Create App ID” on the top right

2

Show up a form and to fill in the following parts:

  • Display Name: this name is the one that you will see when you are on the backstage management platform, and it is also the one that shown on Facebook when the application do any activities for users on Facebook. We fill in “ameba” in this example
  • Contact Email: it should be filled out already if you have logged in
  • Category: according to your application

Then click “Create App ID”

2

Enter backstage management platform after the instructions. There is an APP ID on the top of the screen, and this ID is used to identify the Facebook application. The APP ID is “312698492463829” in this example. You can set up other options of the application on the backstage management platform, such as OAuth, application domain and the launch process.

2

  • Obtain Access Token

Facebook application will act on behalf of the users on Facebook. That is, it is dangerous to let application to get all the permission. As a result, Facebook application needs an access token. Access token is a long string for Graph API to use in HTTP protocol, and Facebook records every access token and its permissions, its time limits. We can manage our own access token via Graph API Explorer: https://developers.facebook.com/tools/explorer
Here is the brief explanation of the backstage management platform:

2

  • See the menu of the application on the top right, and choose “Graph API Explorer”. We can find out ameba application inside the menu after clicking
  • There is a “Get Token” menu under the “Application” menu. You can find out three types of token and we only use “user access token” in this example. Refer to the link about the usage of different access token:
    https://developers.facebook.com/docs/facebook-login/access-tokens
  • On the left of “Token Type” is the token value and is represented by string
  • The following column are the Graph API commands which the users give. From left to right are:
    • HTTP Request type: according to Graph API manual to fill in the desired HTTP Request type. Usually “GET” or “POST”
    • Graph API Version: Facebook will revise Graph API with different versions. The version is v2.8 in this example. It is allowed to not specify the version when you develop the application and Facebook will use the appropriate oldest version by default
    • Request content: fill in the Graph API parameters. Graph API Explorer will help you to add access token depends upon circumstances
  • Debug messages on the center of the screen

Set up the application access token and click “Application”, choose “ameba”

2

Around the place “Get Token”, click “Get User Access Token”

2

Then it will show up a menu where you can choose the actions for users of ameba Facebook application. Because we need to post articles on the Facebook wall, we refer to the Graph API document:

https://developers.facebook.com/docs/graph-api/reference/v2.8/group/feed

https://developers.facebook.com/docs/graph-api/reference/v2.8/user/feed

We need the following permission:
publish_actions, user_managed_groups

2

Then it may show up some warning messages, since Facebook wants to prevent users from being taken advantage of publishing information without the knowledge of the circumstances by the application. As a result, it requests application to provide related policies for users. And we pass this step now in this example unless the application wants to be inspected by Facebook then should we have to make up this part.

2

And you will be asked if you agree ameba Facebook application to post articles for you or not, then click “OK”.

2

You will also be asked if you agree ameba Facebook application to manage groups and fan pages for you or not, then click “OK”.

2

Then go to Graph API Explorer, and the access token gets value now.

2

We take advantage of Graph API Explorer to test if we now can post articles or not. The fields of HTTP request are filled in as follows:

  • HTTP REQ Type: click and choose “POST”
  • HTTP REQ content: write “me/feed?message=HelloWorld” and the message part are the contents to be published. The contents are encoded as URI coding

After clicking “Submit” button, the id of the article appears in the debug messages below.

2

We can take the article id to modify the article, click the Like button and leave comments. The related information of those actions can be found in the Graph API document.

Now we can discover that the ameba Facebook application has already written a post on our Facebook wall. And there is a string “ameba” under the user name of the article.

2

The operation is to send the HTTP protocol information, and to use openssl to simulate. If you have already installed openssl, please execute:

openssl s_client -connect graph.facebook.com:443

Enter interactive mode, and then input the contents shown below (Do not type wrong words and modify or delete them during this step. These actions will be sent as control characters)。The value which is adjacent to “access_token=” is your access token. And the value for “message=” is HelloWorldV2. Notice to input a space at the end to complete the HTTP command.

POST 
/v2.8/me/feed?access_token=EAAEcZAcqQqtUBAAN0YbAemjZCwvmknqjocENiPwUX92TR7Q54jiKrtyEOR8p2EgfR1RQz3uWeDmEQt9oNYOMaRBqGci55CD9chdXcCQXu1jwXPvLpvPZAMeZBVxWZA94sCEZBVN93ZADDe6XkaJpDxJlvdTF0zATFMZD&message=HelloWorldV2
Host: graph.facebook.com

We should get back the messages below

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Pragma: no-cache
Cache-Control: private, no-cache, no-store, must-revalidate
facebook-api-version: v2.8
Expires: Sat, 01 Jan 2000 00:00:00 GMT
Content-Type: text/javascript; charset=UTF-8
x-fb-trace-id: AWAKUnVL1yX
x-fb-rev: 2757773
Vary: Accept-Encoding
X-FB-Debug: 7SqJtuZjt0RZzpAGiknOhcUs0qbDG0Q4WNep592ssnzoc0xRM7IGhsrGy/B38uvGt36vky8rgcfVTD8zuLQ7NA==
Date: Mon, 26 Dec 2016 11:42:36 GMT
Connection: close
Content-Length: 40

{"id":"103964610106403_103999236769607"}closed

One more post on the Facebook wall

2

You can refer to http url encoder to transfer Chinese to URL values if you need to put space or Chinese in the messageshttp://meyerweb.com/eric/tools/dencoder/
Readers can try this part by your own

  • Inspect token and extend token

The access token we got is actually with time limit, and we can use Access Token Debugger to find out the expired time
https://developers.facebook.com/tools/debug/accesstoken/

See the information of the access token, includes the usage by which application, the permission and the expiration (shown by UNIX epoch time). Usually the access token we get from Graph API Explorer is a short-lived token, and it will expire in two hours by default. There is a long-lived access token whose expiration is two months and the time can be extended after the token usage. Click the “Extend Access Token” option on the Access Token Debugger.

2

New token as below, and the value of long-lived token is different with the value of short-lived token. And we can click the Debug on the bottom right to check the long-lived token value

2

The expiration time has already extended to two months

2

Additional notes:

1. According to the Graph API document, Facebook will extend the expiration of the long-lived token to two months at non-regular time if the token is being used. That is, you don’t have to worry about the expiration problem as long as the application makes use of it.
https://developers.facebook.com/docs/facebook-login/access-tokens/expiration-and-extension
However, you have to do the steps to retrieve the new token again if the token isn’t being used until it comes due. And the value of the new token and the old one will not be identical.
2. We can get short-lived token or long-lived token not through the Graph API explorer. Refer to the Graph API document for related details.https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow

  • Post articles on Facebook groups or on fan pages

This part of example is similar with the illustration of writing posts on own Facebook wall. And we just need to get the group id to implement. See the group id on the URL after entering the Facebook group page, the example below is “1210426395661463”.

2

If the names of the Facebook group or fan page have been modified before, you can use some websites to search for the group id:

http://lookup-id.com/

http://wallflux.com/facebook_id/

Next, we fill in the values below via Graph API Explorer

  • HTTP REQ type: “POST”
  • HTTP REQ content: “1210426395661463/feed?message=HelloGroup”
    The string of numbers is group id, and the message is modified as HelloGroup

Show up the article id in the Debug information after you click “Submit” button

2

Go back to the Facebook group page, and you can see that the ameba has already written a post for the user.

2

Example one:Use Ameba to post articles on Facebook wall

In order to let Ameba write a post on Facebook wall, the steps are similar to the implementation of using openssl to simulate posting. You just need to send HTTP protocol contents via SSL.
We will write a post every minute in this example. However, Facebook will review the post contents to check if it is repeated. Therefore, we change the contents every time we post them with a string array. Open the example on Arduino IDE “File” -> “Examples” -> “AmebaWiFi” -> “Facebook” -> “facebook_feed_publish”

Fill in the information of the desired WiFi AP

2

Fill in the access token

2

Set up the target to post. Set as “me” if its own Facebook wall and set as group id if it’s Facebook groups or fan pages

2

Compile and upload to Ameba to execute, then you can find out that Ameba writes a post for the user

2

Example one:Code Reference

  • Variables Reference

String “server” is the server location of Facebook Graph API

char server[] = "graph.facebook.com";    // facebook graph api service

String “access_token” is the short-lived token or the long-lived token. Check the expiration of the short-lived token and the time is usually two hours for short-lived token.

char access_token[] = "EAAEcZAcqQqtUBAAN0YbAemjZCwvmknqjocENiPwUX92TR7Q54jiKrtyEOR8p2EgfR1RQz3uWeDmEQt9oNYOMaRBqGci55CD9chdXcCQXu1jwXPvLpvPZAMeZBVxWZA94sCEZBVN93ZADDe6XkaJpDxJlvdTF0zATFMZD";

Declare the variable “WiFiSSLClient” because we will use SSL in this example

WiFiSSLClient client;

String array “message_list” is the messages that we are going to post and are encoded as URI format. There are many tools for URI encoding on the Internet can convert special characters and symbols.

#define MESSAGE_LIST_SIZE 5
int message_index = 0;
char *message_list[MESSAGE_LIST_SIZE] = {
……
  • Flow chart

Flow chart shown below
2
Remember to check the returned messages from server after sending HTTP POST to Graph API via WiFiSSLClient. The server will return some warning messages if the posting is failed.

Example two:Use Ameba to post PM2.5 messages on the Facebook wall

We will use PMS3003/PMS5003 modules of Plantower in this example. Those modules can detect particulate matter in air and output the PM2.5. You can refer to earlier articles on the website about the usage for Plantower.

We combine PM2.5 to make Ameba release the detected PM2.5 messages in this example. Ameba will write a post about the current PM2.5 and precautions to the Facebook wall when the value variation of PM2.5 is more than 10 or there has been an hour without a new post.

Open “File” -> “Examples” -> “AmebaWiFi” -> “Facebook” -> “facebook_pm25_notifier”

Fill in the information of the desired WiFi AP

2

Fill in the access token

2

Set up the target to post. Set as “me” if its own Facebook wall and set as group id if it’s Facebook groups or fan pages

2

Choose the desired language to post. You can choose Chinese and the default setting is English.

2

After the setup, connect PMS3003 or PMS5003 to Ameba:

2
Compile the source codes and upload to Ameba and the execution is shown below. Ameba will update PM2.5 data at intervals and provide advice for outdoor activities.

2

Example two:Code Reference

This example is similar to the previous one “facebook_feed_publish”except for the PM2.5 part.

We use PMS3003 and the uart of pin 0 and pin 1 to get the PMS3003/PMS5003 PM2.5 value.

PMS3003 pms(0, 1);

Call get_pm25_aircondition() to get PM2.5 value and use get_pm2p5_air() to retrieve PM2.5 data

int pm25 = pms.get_pm2p5_air();

Use moving average to avoid getting unstable value and the formula is as follows:

PM2.5avg = PM2.5avg * 0.9 + PM2.5now * 0.1

Based on the following classification methods and advice to sort in get_pm25_level()

http://taqm.epa.gov.tw/taqm/en/fpmi.aspx

http://taqm.epa.gov.tw/taqm/tw/fpmi.aspx

Program flow chart is shown below:
2

Please confirm that QQ communication software is installed