我正在为一家推广手语的慈善机构工作,他们想每天在其FB页面上发布视频。视频数量众多(并且还在不断增加),因此他们希望以编程方式安排上传时间。我真的不介意我最终会使用哪种编程语言,但是我已经尝试了以下方法,但并没有走得太远:

  • Perl使用 WWW::Facebook::API (旧的REST API)
    my $res = $client->video->upload(
     title => $name,
     description => $description,
     data => scalar(read_file("videos/split/$name.mp4"))
    );
    

    身份验证正常,这可以将facebook.video.upload方法正确地发布到https://api-video.facebook.com/restserver.php。不幸的是,这返回“方法未知”。我认为这与REST API被弃用有关。
  • Perl中的
  • Facebook::Graph 或Ruby中的fb_graph gem。 (OAuth API)

    我什至无法验证。这两个都是针对OAuth的Web应用程序而非桌面应用程序的,但我认为我应该能够做到:
    my $fb = Facebook::Graph->new(
     app_id => "xxx",
     secret => "yyy",
     postback => "https://www.facebook.com/connect/login_success.html"
    );
    print $fb->authorize->extend_permissions(qw(publish_stream read_stream))->uri_as_string;
    

    在我的浏览器中转到该URL,捕获返回的code参数,然后
    my $r = $fb->request_access_token($code);
    

    不幸:
    Could not fetch access token: Bad Request at /Library/Perl/5.16/Facebook/Graph/AccessToken/Response.pm line 26
    
  • 同样在Ruby中,使用 fb_graph
    fb_auth = FbGraph::Auth.new(APP_ID, APP_SECRET)
    
    client = fb_auth.client
    client.redirect_uri = "https://www.facebook.com/connect/login_success.html"
    puts client.authorization_uri(
      :scope => [:publish_stream, :read_stream]
    )
    

    给我一个返回代码但正在运行的URL
    client.authorization_code = <code>
    FbGraph.debug!
    access_token = client.access_token!
    

    退货
    {
      "error": {
        "message": "Missing client_id parameter.",
        "type":    "OAuthException",
        "code":    101
      }
    }
    

    更新:当我将access_token!调用更改为access_token!("foobar")以强制Rack::OAuth2::Client将标识符和 secret 放入请求正文中时,出现以下错误:
    {
      "error": {
        "message": "The request is invalid because the app is configured as a desktop app",
         "type":   "OAuthException",
         "code":   1
      }
    }
    

  • 我应该如何使用OAuth向Facebook认证桌面/命令行应用程序?

    最佳答案

    因此,我终于在不设置Web服务器和执行回调的情况下正常运行了。违反直觉的诀窍是关闭“桌面应用程序”设置,而不是请求offline_access
    FaceBook::Graph对发布视频的支持目前似乎无法正常工作,因此我最终在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
    )
    

    关于ruby - 为命令行(桌面)应用程序获取Facebook身份验证 token ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21978728/

    10-10 21:25