为命令行(桌面)应用程序获取 Facebook 身份验证令牌 [英] Obtaining a Facebook auth token for a command-line (desktop) application
问题描述
我在一家推广手语的慈善机构工作,他们想每天在他们的 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 使用
WWW::Facebook::API
(旧的 REST API)
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.
Facebook::Graph
Perl 或 fb_graph
Ruby 中的 gem.(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,捕获返回的 code
参数,然后
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屋!