Get Facebook long live access token in AWS Lambda function

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. 

Also Read: Multiple user roles authentication Laravel 8

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'
}
}

Also Read: Deploy Laravel project from local to production server

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. 

Also Read: Multiple user roles authentication Laravel 8

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'
}
}

Also Read: Deploy Laravel project from local to production server

Please let me know what your thoughts or comments are on this article. If you have any suggestion or found any mistake in this article then please let us know.

Latest Comments

Add your comment

Close