How to programmatically log in to Facebook oauth2 api

Story:

I wrote a Node.JS script that successfully connects to the Facebook API through my facebook app. I can read the data when I give it oauth access_token, I want this script to run on my server every night to store some data. I did a lot of research on both facebook api, oauth, and similar stack overflow issues. I'm looking for an endpoint/search/?type=event&q=query

Problem:

However, Facebook returns 60 days access_tokenthrough the oauth2 registration process, which required me to create a server expressthat simply initiates the oauth2 process, allows the user to log in and get the code, access_tokenand I store it.

I want the script to save data so that my server can provide access to updated data every day. I do not want you to forget to log in to generate a key once every 60 days.

Question:

Is there anyway to get oauth2 access_tokenwithout setting up a server httpor express? More importantly, how do I get access_tokenwithout having to manually start this server every ~ 60 days.

the code:

For the module that I use, are required access_tokenandclient_secret

fs.readFile('./facebookAuthServer/oauth.txt', function read(err, data) {
    if (err) {
        throw err;
    }
    fbNode.setAuthorization({token: data, clientSecret: authSettings.clientSecret});
    // Use the auth for next call
    fbNode.fetchItems(displayItems);
});

Is there a way to trick the headers? or can I use a short-term access token and renew it? In any case, to update the 60-day token? Has anyone created an Oauth2 server-side implementation that does not require visiting the FB login more than the first time?

+4
2

, request.

OAuth:

var express = require('express')
var session = require('express-session')
var Grant = require('grant-express')

var grant = new Grant({
    server:{host:'dummy.com:3000', protocol:'http'},
    facebook:{
      key:'[APP_ID]',
      secret:'[APP_SECRET]',
      scope:['user_about_me','user_birthday'],
      callback:'/callback'
    }
  })

var app = express()
app.use(session({secret:'very secret'}))
app.use(grant)

app.get('/callback', function (req, res) {
  res.end(JSON.stringify(req.query))
})

app.listen(3000, function () {
  console.log('Oh Hi', 3000)
})

HTTP-, :

var request = require('request')

request.get({
  uri:'http://dummy.com:3000/connect/facebook',
  headers:{
    'user-agent':'Mozilla/5.0 ...',
    cookie:'datr=...; lu=...; p=-2; c_user=...; fr=...; xs=...; ...'
  },
  jar:request.jar(),
  json:true
}, function (err, res, body) {
  if (err) console.log(err)
  console.log(body)
})

:

  • OAuth Facebook URL- ( http://dummy.com:3000)
  • 127.0.0.1 dummy.com hosts
  • dummy.com:3000 .
  • , , Preserve log
  • http://dummy.com:3000/connect/facebook
  • HTTP- (user-agent cookie)
  • HTTP ( , , )

:

:

https://github.com/simov/facebook-refresh-token

+4

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


All Articles