0

What i try to do is retrieve my facebook leads from my facebook page in a webhook ASP.net core API , get the lead details and save it in my mongoDB Database.

I had a problem getting my lead details in the API because of the token.

And then i saw in the facebook developer docs : a. Get a regular user token. b. Convert it into a long-lived token c. With the long-lived user's access token, request the page token [..] This long-lived page token has no expiration date; you can hard-code it in simple RTU integrations to get leads data.

The problem is i got that page token like they show in their docs , but it expired one hour after i got it. But they say here it's not expiring. So i don't understand how should i do.

What i did is getting the token from the web , saving it in my database and when a lead comes search it in my database to do the request to get the lead details of the page.

I tried with a app token but i had an error that i need a user token but getting a user token from an API doesn't seems correct.

Tell me if i miss something. Really need your help here ! Thanks !

I have my Angular web and my ASP.net core API . I tried with a app token , and a page token gotten from the web.

Web :

///Login

FB.login(response => {
  if (response.authResponse) {
    this.userId = response.authResponse.userID;
    FB.api('me/accounts', responsePages => {
      this.ngzone.run(() => {
        this.pages = responsePages.data.map(elem => {
          let page = new FacebookPage();
          page.access_token = elem.access_token;
          page.category = elem.category;
          page.id = elem.id;
          page.name = elem.name;
          FB.api(page.id + "/subscribed_apps?access_token=" + page.access_token, res => {
            if (res.data.length > 0 && res.data[0].name === facebookAPPName && res.data[0].id === facebookAPPId)
              page.connected = true;
            else page.connected = false;
          })
          return page;
        });    
      });
    })
  }
  else {
    console.log('User login failed');
  }
}, { scope: ['manage_pages', 'leads_retrieval'] });

///Link Page

FB.api(this.userId+"/accounts", res => {
      let accessToken = res.data.filter(elem => elem.name === page.name[0].access_token;
       FB.api('/' + page.id + '/subscribed_apps',
      'post',
      { access_token: page.access_token, subscribed_fields: ['leadgen'] },
       (response) => {
        console.log('Successfuly subscribed ' + JSON.stringify(response))
        page.connected = true;
        page.access_token = accessToken;
        this.facebookService.link(page).subscribe(()=>{
                  this.spinner.hide();
        },
        err => { console.log(err); this.spinner.hide();})
      })
    })

API :

[HttpPost]
        public IActionResult Post([FromBody]FacebookLead data)
        {
            var entry = data.entry.FirstOrDefault();
            var change = entry?.changes.FirstOrDefault();

            var leadId = change.value.leadgen_id;

            var client = new HttpClient();

            var page = this.facebookService.Get(change.value.page_id);

            var request = client.GetAsync("https://graph.facebook.com/v3.3/" + leadId + "?access_token=" + page.access_token);

            request.Wait();

            var res = request.Result.Content.ReadAsStringAsync();

            res.Wait();

            var result = res.Result;

            return Ok();
        }

Expected output is the detail of the lead , without expiration of token in API. But i get an error either because the token expired or because app token doesn't work

  • If your page token expires this quickly, then you likely did not perform step b correctly. Debug the user access token you are using to request the page token, and see what it says about its expiry. – misorude Jul 09 '19 at 06:58
  • @misorude i'm quite new in this and i'm in angular. I edited my post and added all the web code. I first do the first part (login) where i log in and take all of the pages of the user and check if they are connected , and then when the user wants to link a page i searched for the page token as requisted in the docs and call the api to save the token in my database. But yeah this token expires – Sacha Krief Jul 09 '19 at 07:49
  • I don’t see you performing step b at all. – misorude Jul 09 '19 at 07:52
  • @misorude okay it could work , but what it's saying when on the access token debugger it says "expires : never" but " Data Access Expires : In about 3 months" ? what is Data Access Expires ? – Sacha Krief Jul 09 '19 at 08:33
  • https://developers.facebook.com/docs/facebook-login/auth-vs-data – misorude Jul 09 '19 at 08:35
  • @misorude yeah i saw that but i didn't understand a lot .. if i use this token to get details of a lead from the page from lead id , do i need to look at expires or data access expires ? – Sacha Krief Jul 09 '19 at 08:38
  • Both …? If your token expires, the API won’t accept your request at all. And if access to specific data has expired, you won’t be able to request that specific data any more. – misorude Jul 09 '19 at 08:44
  • @misorude so how can i do in my case ? If the access token have his data access expires in three months i will not be able to get the details of the lead three months after .. There is no token which doesn't have data access token who doesn't ever expires ? – Sacha Krief Jul 09 '19 at 08:50
  • `manage_pages` isn’t affected by data access expiry, `leads_retrieval` however is not listed as one of the exceptions, so that one probably is. Worst case scenario - you have to interact with your app every 90 days, and refresh its access to your data. – misorude Jul 09 '19 at 08:59
  • @misorude so you think i will have to get a new token every 90 days if i want to use it to get the details of a lead – Sacha Krief Jul 09 '19 at 10:02
  • Unless you are otherwise “active” in your app, performing some actions using the user access token – then the access would not expire to begin with. _“The expiration period for data access is 90 days, based on when the user was last active.”_ – misorude Jul 09 '19 at 10:08
  • @misorude what do you call by "active" ? If let say i link new pages to app , things like that , the page token i previously had will have their data access expiry later than it was before ? – Sacha Krief Jul 09 '19 at 11:15
  • That is Facebook’s own phrasing, that’s why I quoted it. The documentation on Rate Limits uses the same phrasing, but does not explain what exactly “active” means either. If a mere API call was sufficient though, then every developer could prevent data access expiry by regularly making requests using a stored token, and that would kinda defeat the whole purpose. I guess the user needs to actively log in, or visit a website that uses the app and has the JS SDK embedded, something like that. – misorude Jul 09 '19 at 11:21
  • Guess you best keep it simple, and set up some sort of notification, like an e-mail that gets send by your app, if either the token or the data access have expired - and then simply go through the login flow once again with your user account. – misorude Jul 09 '19 at 11:24
  • @misorude Okay so you think if i just login once again as i did in the code i gave you , the data access expires of all the token of all the pages will refresh himself. Because it's written in your link that i need to call with auth_type: 'reauthorize' , but i don't have it on my regular login – Sacha Krief Jul 10 '19 at 07:25
  • If it says you need to use reauthorize, then that’s probably the case, yeah. _“but i don't have it on my regular login”_ - but you control the code that triggers it, so that you could add it somehow? (And if not, adding `&authtype=reauthorize` at the end of the URL when the login dialog is shown, might also work.) – misorude Jul 10 '19 at 07:34
  • @misorude i used the FB.login function. I don't know more about it. And if i use reauthorize and it's a user that never connected before it will not do an issue ? – Sacha Krief Jul 10 '19 at 07:38
  • https://developers.facebook.com/docs/reference/javascript/FB.login/v3.3 – misorude Jul 10 '19 at 07:40
  • But no, you’re not supposed to use that auth type “preemptively” on every login. You can try if it works without issues, but I don’t think its recommended. If this is an app that’s not used by the general public to begin with, then maybe add a checkbox or something in the place where you trigger the login, so that you can add the authtype based on that. – misorude Jul 10 '19 at 07:43

0 Answers0