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

后端 未结 4 1601
梦谈多话
梦谈多话 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:09

    Problem is in your case that for OAuth you'll need some endpoint URL which is publicly reachable over the Internet for Facebook servers, which can be a no-go for normal client PCs, or a desktop application which is capable of WebViews (and I assume, command line isn't).

    Facebook states at https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow#login that you can build a desktop client login flow, but only via so-called WebViews. Therefore, you'd need to call the OAuth endpoint like this:

    https://www.facebook.com/dialog/oauth?client_id={YOUR_APP_ID}&redirect_uri=https://www.facebook.com/connect/login_success.html&response_type=token&scope={YOUR_PERMISSION_LIST}
    

    You then have to inspect the resulting redirected WebView URL as quoted:

    When using a desktop app and logging in, Facebook redirects people to the redirect_uri mentioned above and places an access token along with some other metadata (such as token expiry time) in the URI fragment:

    https://www.facebook.com/connect/login_success.html#access_token=ACCESS_TOKEN... 
    

    Your app needs to detect this redirect and then read the access token out of the URI using the mechanisms provided by the OS and development framework you are using.

    If you want to do this in "hacking mode", I'd recommend to do the following.:

    • As you want to post to a Page, get a Page Access Token and store it locally. This can be done by using the Graph Explorer at the

    https://developers.facebook.com/tools/explorer?method=GET&path=me%2Faccounts

    endpoint. Remember to give "manage_pages" and "publish_actions" permissions.

    • Use cURL (http://curl.haxx.se/docs/manpage.html) to POST the videos to the Graph API with the Access Token and the appropriate Page ID you acquired in step 1 like the following:

    curl -v -0 --form title={YOUR_TITLE} --form description={YOUR_DESCRIPTION} --form source=@{YOUR_FULL_FILE_PATH} https://graph-video.facebook.com/{YOUR_PAGE_ID}/videos?access_token={YOUR_ACCESS_TOKEN}

    References:

    https://developers.facebook.com/docs/graph-api/reference/page/videos/#publish https://developers.facebook.com/docs/reference/api/video/

提交回复
热议问题