How to connect to Facebook API from Google Apps Script?

I try to connect to the Facebook API via Google Apps Script, but I get an error

I tried:

function myFunction (name) { FB.init({ appId : '{your-app-id}', status : true, xfbml : true, version : 'v2.0' }); var jsonData = UrlFetchApp.fetch("graph.facebook.com/"; + name); } 

I also tried:

 function myFuntion(name) { window.fbAsyncInit = function() { FB.init({ appId : 'your-app-id', xfbml : true, version : 'v2.0' }); }; var jsonData = UrlFetchApp.fetch("graph.facebook.com/"; + name); } 

but none of them worked, I always get:

"ReferenceError:" FB "is not defined." and "ReferenceError:" window "undefined"
and
"(# 4) Application request limit reached", "type": "OAuthException", "code": 4}}

even though I entered my facebook app id in a variable. I know that the โ€œwindowโ€ is part of the external javascript library, so I canโ€™t use it in Google Apps Script, but even after looking at other messages Iโ€™m still confused about why I get the โ€œFBโ€ error is not defined.

Any ideas on how to solve this?

+6
source share
1 answer

At the bottom of this page are error codes:

Facebook Graph API - Error Codes

" OAuthException " refers to the login status. If you get this error, then you are not logged in and get what you want, you need to log in.

You can get the access token for applications using a Server to Server request. There are four types

Access Tokens:

  • User - to read, modify or write data of a specific person on Facebook on their behalf.
  • App - change and read application settings and publish Open Graph actions.
  • Page - Read, write or modify data belonging to a Facebook page.
  • Client - a client token is rarely used. Very limited access to Facebook.

Access Token Forms

User access tokens come in two forms: short-lived tokens and long-lived tokens

  • short-lived - service life of about an hour or two - generated via web login
  • durable - service life of about 60 days

You probably don't have an application access token. You have an application id, but this is different from App Token.

You only get your application token once. You need to run the code to get it.

Please note that you must also know your App Secret to run this code. If you do not know or have your application secret, then you need to get it.

See if you can run this code:

 //A Facebook App Token never changes unless you go to the Facebook Developers Console, //and you //change the App Secret. So, do NOT keep requesting a new App Token. Just get it once, //then //hard code it into a backend secret function. // The App Token can be used to modify your App, but you can just do that 'Manually' function getOneTimeFB_AppToken() { Logger.log("getOneTimeFB_AppToken ran"); //keep this info secret //Generate an App Access Token var myApp_ID = 'Your App ID'; var myAppSecret = 'Your App Secret'; var optnAppTkn = {"method" : "get"}; var getAppTknURL = "https://graph.facebook.com/oauth/access_token?client_id=" + myApp_ID + "&client_secret=" + myAppSecret + "&grant_type=client_credentials" var getAppTkn = UrlFetchApp.fetch(getAppTknURL, optnAppTkn); Logger.log("Object returned from GET: " + getAppTkn) var myAppTkn = getAppTkn.getContentText(); Logger.log("myAppTkn: " + myAppTkn); }; 

Run this code, then in the script editor select the VIEW menu and the LOGS menu item. Read what's on LOGS. Do not use this code again and again. Just run it once if it is successful.

If this code works, then you just chatted with Facebook.

You need to understand what tokens can do, and what are your options. If you are not going to receive a token from the user through authorization on the client side, you need to understand App Token.

App Tokens allows you to interact with Facebook on behalf of the application, not as a user. This can be used to read YOUR applications and change YOUR parameters.

You never want to use the application token in the client part (browser). This will be a serious security issue.

However, if the user has granted permission to publish your application, you can use the App Token to publish content on Facebook on behalf of this person. Thus, an application access token can be used instead of a user access token to make API calls, if your application has permission to publish.

But how do you get permission to publish? Well, there is no way to get the initial short-term access token through the server. It makes sense if you think about it from a security point of view. You cannot receive an initial, short-term access token in any other way than through the clientโ€™s login. So, if you want to do something that is not included in the scope of the application access token, you cannot do this without entering the user through the client side.

You can log in on the client side without using the JavaScript SDK. Thus, in the case of the script Stand Alone HTML application for apps, you can still use Facebook login without having to link to the Facebook SDK. If you need to do this, let me know.

In this code, FB is an object. The object must be assigned key / value pairs. Each key / value pair is an element (property) of an object. The error is directly related to how objects work. This FB object receives the assigned values โ€‹โ€‹from the link (inside HTML) in the Facebook API. If you try to use the HTML link to the Facebook API from the server (.gs), this will not work. There are many things that can go wrong. To know exactly what is going wrong, we need to know whether this code is in the gs file or the HTML file inside the <script> .

There are several ways to connect to Facebook:

  • From HTML (client side)
  • On a server with HTTP requests

It looks like the code you are using is an example of using the JavaScript SDK for Facebook, designed to run from within HTML. The problem is that the script applications deactivate the HTML sent to the browser. So, if you try to contact the Facebook JavaScript SDK via HTML, you will not be able to access. I know that in the past I was not able to use the Facebook API link in HTML format with the sandbox mode. I have not tried the new IFRAME sandbox mode.

+9
source

Source: https://habr.com/ru/post/982583/


All Articles