获取命令行(桌面)应用程序的Facebook身份验证令牌 [英] Obtaining a Facebook auth token for a command-line (desktop) application

查看:206
本文介绍了获取命令行(桌面)应用程序的Facebook身份验证令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为促进手语的慈善事业工作,他们每天都要向他们的FB页面发布一个视频。有大量(并且不断增长)的视频,所以他们想以编程方式安排上传。我真的不介意我最终做了什么编程语言,但是我尝试过以下操作,并没有太远:

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 they want to schedule the uploads programmatically. I don't really mind what programming language I end up doing this in, but I've tried the following and not got very far:

  • Perl using WWW::Facebook::API (old 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有关。

Authentication is OK, and this correctly posts a facebook.video.upload method to https://api-video.facebook.com/restserver.php. Unfortunately, this returns "Method unknown". I presume this is to do with the REST API being deprecated.

fb_graph gem中的3aGraph> Facebook :: Graph 。 (OAuth API)

Facebook::Graph in Perl or fb_graph gem in Ruby. (OAuth API)

我甚至不能认证。这两个都适用于网络而不是OAuth的桌面应用程序,但我认为我应该能够做到:

I can't even authenticate. Both of these are geared towards web rather than desktop applications of OAuth, but I think I ought to be able to do:

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,捕获代码参数返回,然后

Go to that URL in my browser, capture the code parameter returned, and then

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它返回代码,但运行

    Gives me a URL which returns a code, but running

    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放置标识符和秘密进入请求正文,我收到以下错误:

    Update: When I change the access_token! call to access_token!("foobar") to force Rack::OAuth2::Client to put the identifier and secret into the request body, I get the following error instead:

    {
      "error": {
        "message": "The request is invalid because the app is configured as a desktop app",
         "type":   "OAuthException",
         "code":   1
      }
    }
    


  • 使用OAuth验证桌面/命令行应用程序到Facebook?

    How am I supposed to authenticate a desktop/command line app to Facebook using OAuth?

    推荐答案

    所以,我终于得到它的工作,没有设置Web服务器并进行回调。这个伎俩,反直觉地是关闭桌面应用程序设置,而不要求 offline_access

    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 支持发布视频目前似乎不起作用,所以我最终在Ruby中做到这一点。

    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
    )
    

    这篇关于获取命令行(桌面)应用程序的Facebook身份验证令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆