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