Get Facebook long live access token in AWS Lambda function
First create login with facebook button in your frondend app or website. You can create facebook login button from their official developer site.
https://developers.facebook.com/docs/facebook-login/web/login-button
Create button, click on get code and paste it in your frontend site. You can adjust width, change text, layout and size of the button.
Don't forgot to change app id in the above generated code.
When user clicked on the above button, a facebook login popup window will open. After enter correct credentials user will redirect to your lamda gateway your which you have to mentioned in your app.
A temporary access token will generated after login successful. If you need a long-lived User access token you can generate one from temprary access token. A long-lived token generally lasts about 60 days.
You will need the following to get long-lived access token:
- temprary access token
- App ID
- App Secret
Use below code in your lamda function to get user details with long access token.
import requests
import json
def main(event, context):
#get post data
getData = json.loads(event['body'])
access_token = getData['access_token']
clientId = 'ENTER CLIENT ID HERE'
secretKey = 'ENTER SECRET KEY HERE'
getLongAccessTokenUrl = "https://graph.facebook.com/v11.0/oauth/access_token?grant_type=fb_exchange_token&client_id="+clientId+"&client_secret="+secretKey+"&fb_exchange_token="+access_token
getLongTokenResponse = requests.get(getLongAccessTokenUrl)
longTokenIs = getLongTokenResponse.json()['access_token']
#get request to get userinfo using long access token
requestUrl = "https://graph.facebook.com/me?access_token="+longTokenIs
getUSerInfo = requests.get( requestUrl)
userId = getUSerInfo.json()['id']
fb_user_name = getUSerInfo.json()['name']
return {
'statusCode': 200,
'body': longTokenIs,
"headers": {
'Content-Type': 'text/html',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
}
}
First create login with facebook button in your frondend app or website. You can create facebook login button from their official developer site.
https://developers.facebook.com/docs/facebook-login/web/login-button
Create button, click on get code and paste it in your frontend site. You can adjust width, change text, layout and size of the button.
Don't forgot to change app id in the above generated code.
When user clicked on the above button, a facebook login popup window will open. After enter correct credentials user will redirect to your lamda gateway your which you have to mentioned in your app.
A temporary access token will generated after login successful. If you need a long-lived User access token you can generate one from temprary access token. A long-lived token generally lasts about 60 days.
You will need the following to get long-lived access token:
- temprary access token
- App ID
- App Secret
Use below code in your lamda function to get user details with long access token.
import requests
import json
def main(event, context):
#get post data
getData = json.loads(event['body'])
access_token = getData['access_token']
clientId = 'ENTER CLIENT ID HERE'
secretKey = 'ENTER SECRET KEY HERE'
getLongAccessTokenUrl = "https://graph.facebook.com/v11.0/oauth/access_token?grant_type=fb_exchange_token&client_id="+clientId+"&client_secret="+secretKey+"&fb_exchange_token="+access_token
getLongTokenResponse = requests.get(getLongAccessTokenUrl)
longTokenIs = getLongTokenResponse.json()['access_token']
#get request to get userinfo using long access token
requestUrl = "https://graph.facebook.com/me?access_token="+longTokenIs
getUSerInfo = requests.get( requestUrl)
userId = getUSerInfo.json()['id']
fb_user_name = getUSerInfo.json()['name']
return {
'statusCode': 200,
'body': longTokenIs,
"headers": {
'Content-Type': 'text/html',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
}
}
Recommanded Articles
- Get Facebook long live access token in AWS Lambda function
- How to make image background transparent using PHP
- Paypal credit card payment on website in PHP
- Capture page screenshot by URL PHP script
- How to get all UK addresses by postal code
- API versioning in Laravel App
- Get all the lists from Mailchimp account using CURL
- Create a LEAD in ZOHO CRM using Node JS
- Create a LEAD in ZOHO CRM using API
- Add new subscriber in a Klaviyo list API
Latest Comments