Obtaining a Facebook auth token for a command-line (desktop) application

后端 未结 4 1598
梦谈多话
梦谈多话 2020-12-17 09:38

I am working for a charity which is promoting sign language, and they want to post a video to their FB page every day. There\'s a large (and growing) number of videos, so th

4条回答
  •  粉色の甜心
    2020-12-17 10:15

    So, I finally got it working, without setting up a web server and doing a callback. The trick, counter-intuitively, was to turn off the "Desktop application" setting and not to request offline_access.

    FaceBook::Graph's support for posting videos doesn't seem to work at the moment, so I ended up doing it in Ruby.

    fb_auth = FbGraph::Auth.new(APP_ID, APP_SECRET)
    
    client = fb_auth.client
    client.redirect_uri = "https://www.facebook.com/connect/login_success.html"
    
    if ARGV.length == 0
      puts "Go to this URL"
      puts client.authorization_uri(:scope => [:publish_stream, :read_stream] )
      puts "Then run me again with the code"
      exit
    end
    
    if ARGV.length == 1
      client.authorization_code = ARGV[0]
      access_token = client.access_token! :client_auth_body
      File.open("authtoken.txt", "w") { |io| io.write(access_token)  }
      exit
    end
    
    file, title, description = ARGV
    
    access_token = File.read("authtoken.txt")
    fb_auth.exchange_token! access_token
    File.open("authtoken.txt", "w") { |io| io.write(fb_auth.access_token)  }
    
    me = FbGraph::Page.new(PAGE_ID, :access_token => access_token)
    me.video!(
        :source => File.new(file),
        :title => title,
        :description => description
    )
    

提交回复
热议问题