你怎么用法拉第使用asana api?

时间:2014-08-27 22:30:21

标签: api asana faraday

我正在尝试用法拉第宝石连接到Asana api。我的http请求使用curl工作但我得到了与法拉第完全不同的响应。我做错了什么:

require 'faraday'
require 'base64'
require 'net/https'

module Asana
  def self.get_tasks(project_asana_id)
    conn = create_connection
    response = conn.get "/tasks", {"project" => 14790966979027}, {"Content-Type" => "text/plain", "User-Agent"=>"Chrome/25.0.1364.160", "Accept" => "application/json"}
    p response.body
  end

  def self.create_connection
    Faraday.new(:url => ENV['ASANA_URL']) do |faraday|
      faraday.request  :url_encoded             # form-encode POST params
      faraday.response :logger                  # log requests to STDOUT
      faraday.request :basic_auth, ENV['ASANA_API_KEY'], ''
      faraday.adapter  :net_http  # make requests with Net::HTTP
    end
  end
end

我的回答:

"<html>\n<head>\n<title>Asana - Log In</title><script>__FILE__=\"(none)\";var config = {\n\"CLUSTER\": \"prod\",\n\"PRETTY_JS_CODEGEN\": false\n};</script><link rel=\"shortcut icon\" href=\"/-/static/luna/browser/images/favicon.ico\" />\n\n<script type=\"text/javascript\" src=\"https://use.typekit.com/sli4yxq.js\"></script><script>__FILE__=\"(none)\";try{Typekit.load();}catch(e){}</script>\n<script>__FILE__=\"(none)\";req = reqLazy = function(file_path) {   var m = modules[file_path];   return (m !== undefined) ? m.exports : undefined; }; modules = {}; globals = window;</script>\n<link type=\"text/css\" rel=\"stylesheet\" href=\"/-/static/build/css/apps/asana/css/dialog_page_root.css\" />\n<link type=\"text/css\" rel=\"stylesheet\" href=\"/-/static/build/css/apps/asana/css/login.css\" />\n<script>__FILE__=\"3rdparty/mochikit/MochiKit/Base.js\";module = { id: __FILE__, exports: {} };exports = module.exports;modules[module.id] = module;</script>\n<script type=\"text/javascript\" src=\"/-/static/3rdparty/mochikit/MochiKit/Base.js\" charset=\"utf-8\"></script>\n<script>__FILE__=\"3rdparty/mochikit/MochiKit/Iter.js\";module = { id: __FILE__, exports: {} };exports = module.exports;modules[module.id] = module;</script>\n<script type=\"text/javascript\" src=\"/-/static/3rdparty/mochikit/MochiKit/Iter.js\" charset=\"utf-8\"></script>\n<script>__FILE__=\"3rdparty/mochikit/MochiKit/Async.js\";module = { id: __FILE__, exports: {} };exports = module.exports;modules[module.id] = module;</script>\n<script type=\"text/javascript\" src=\"/-/static/3rdparty/mochikit/MochiKit/Async.js\" charset=\"utf-8\"></script>\n<script>__FILE__=\"3rdparty/mochikit/MochiKit/DOM.js\";module = { id: __FILE__, exports: {} };exports = module.exports;modules[module.id] = module;</script>\n<script type=\"text/javascript\" src=\"/-/static/3rdparty/mochikit/MochiKit/DOM.js\" charset=\"utf-8\"></script>\n<script>__FILE__=\"3rdparty/mochikit/MochiKit/Style.js\";module = { id: __FILE__, exports: {} };exports = module.exports;modules[module.id] = module;</script>\n<script type=\"text/javascript\" src=\"/-/static/3rdparty/mochikit/MochiKit/Style.js\" charset=\"utf-8\"></script>\n<script>__FILE__=\"luna/common/lib/minimochi.js\";module = { id: __FILE__, exports: {} };exports = module.exports;modules[module.id] = module;</script>\n<script type=\"text/javascript\" src=\"/-/static/luna/common/lib/minimochi.js\" charset=\"utf-8\"></script>\n</head>\n<body>\n<!-- DEBUG-TAG: login -->\n<div id=\"Flags\" class=\"enable_experiment_enrollment_logging enable_delete_session_objects enable_oauth enable_new_billing_page enable_expanded_team_browser log_api_metrics_to_graphite enable_separator_between_far_apart_stories enable_mobile_sections enable_rph_search enable_rph_search_topbar enable_fast_query_dependencies enable_list_caching enable_reset_view_state_on_switches enable_task_caching enable_asanarama enable_key_combos enable_promote_tasks_with_due_dates enable_sort_by_alpha enable_hypertext_ime enable_spooky_theme enable_winter_theme enable_google_tags_iframe enable_box_attachments enable_hacks enable_sync_without_promises\"><script type=\"text/javascript\" src=\"/-/static/luna/browser/browser_google_auth.js\"></script><script>BrowserGoogleAuth.init();</script><script type=\"text/javascript\" src=\"/-/static/luna/browser/dialog.js/?v=3\"></script><div id=\"dialog_wrapper\"><div id=\"bg_pattern\" class=\"bg-pattern\"></div><script>\n  if (navigator.userAgent.indexOf('WebKit') > 0) {\n    document.getElementById(\"dialog_wrapper\").className += ' webkit';\n  }\nDialog.event_name_to_url[\"BrowserLogin-GoogleAuth-Redirect\"] = \"https://app.asana.com/app/asana/-/log?se=%7B%22name%22%3A%22BrowserLogin-GoogleAuth-Redirect%22%2C%22user_agent%22%3A%22Chrome%2F25.0.1364.160%22%7D&cb=1409177825638&hash=08fa47a0cd77d30807017aea9003b9803d22525eb78f4af4f51d8de4c29094ff\";\nDialog.event_name_to_url[\"BrowserLogin-GoogleAuth-Popup-Start\"] = \"https://app.asana.com/app/asana/-/log?se=%7B%22name%22%3A%22BrowserLogin-GoogleAuth-Popup-Start%22%2C%22user_agent%22%3A%22Chrome%2F25.0.1364.160%22%7D&cb=1409177825642&hash=ee5cf64e6b5c72f7c8ba4bf62dd63adf5974fba3dfc8393eff7ae9c5601a0c45\";\nDialog.event_name_to_url[\"BrowserLogin-GoogleAuth-Popup-NotAuthorized\"] = \"https://app.asana.com/app/asana/-/log?se=%7B%22name%22%3A%22BrowserLogin-GoogleAuth-Popup-NotAuthorized%22%2C%22user_agent%22%3A%22Chrome%2F25.0.1364.160%22%7D&cb=1409177825644&hash=cf830e279ad33bc2190b41ad25e8374968655300e4376fbde003bb2ccf31d927\";\nDialog.event_name_to_url[\"BrowserLogin-GoogleAuth-Popup-Authorized\"] = \"https://app.asana.com/app/asana/-/log?se=%7B%22name%22%3A%22BrowserLogin-GoogleAuth-Popup-Authorized%22%2C%22user_agent%22%3A%22Chrome%2F25.0.1364.160%22%7D&cb=1409177825645&hash=b771dc185c05ea808f8b189c1036175ab7c1988928ef6d72f1297170bed46f9c\";</script><div class=\"dialog-page auto-fill-height-0\"><div class=\"formpage-header\"><div class=\"formpage-spacer\"></div><a href=http://www.asana.com/?utm_source=unknown&amp;utm_campaign=app.asana.com><img class=\"large-logo\" src=\"/-/static/apps/asana/view/large_logo.png\"></a></div><div class=\"sidebars\" id=\"sidebars\"><div class=\"dialog-container clearfix\" id=\"dialog-container\"><div class=\"formpage-content\"><div class=\"title\">Log In</div><div class=\"title-bottom\"><span/></div><div class=\"dialog-text\"><table width=\"100%\"><tr><td class=\"content\"><form id=\"dialog_form\" action=\"https://app.asana.com/app/asana/-/login\" method=\"post\"><input type=\"hidden\" name=\"u\" value=\"https://app.asana.com/tasks?project=14790966979027\"/><input type=\"hidden\" name=\"i\" value=\"password\"><input type=\"hidden\" name=\"src\" value=\"login\"/><input type=\"hidden\" name=\"auth\"/><input type=\"hidden\" name=\"xsrf_token\" value=\"0cebe1ed6682122da9fd6a68d061ee6b:1409177825636\"/><div class=\"form-view\"><table><tr id=\"google_auth_row\"><td/><td class=\"buttons\" colspan=\"2\"><div tabindex=0 class=\"asana-button enabled primary button\" id=\"google_auth_button\" onclick=\"return Dialog.Login.loginWithGoogle(false, 'https://app.asana.com/-/oauth2callback', 'https://app.asana.com/tasks?project=14790966979027')\"><span class=\"button-text\">Log In with Google Account</span></div></td></tr><tr><td colspan=\"3\"><div id=\"google_auth_separator\" class=\"separator login-page-separator\"><div class=\"or_separator\">or</div></div></td></tr><tr id=\"email_row\"><td class=\"field-name email\">Email Address</td><td class=\"field-value email\"><input class=\"generic-input showing\" type=\"email\" name=\"e\" id=\"email_input\" value=\"\" onfocus=\"Dialog.setFocusedRow('email_row', true);\" onblur=\"Dialog.setFocusedRow('email_row', false);\"/></td><td class=\"field-no-status\"></td></tr><tr><td colspan=\"2\" class=\"spacer\"/></tr><tr id=\"password_row\"><td class=\"field-name password\">Password</td><td class=\"field-value password\"><input class=\"generic-input showing\" type=\"password\" name=\"p\" id=\"password_input\" onfocus=\"Dialog.setFocusedRow('password_row', true);\" onblur=\"Dialog.setFocusedRow('password_row', false);\"/></td><td class=\"field-no-status\"></td></tr><tr><td></td><td class=\"field-description\"><a href=\"/-/forgot_password\" tabindex=\"-1\">Forgot your password?</a></td></tr><tr><td></td><td class=\"buttons\" colspan=\"2\"><div tabindex=0 class=\"asana-button enabled primary button\" id=\"submit_button\" onclick=\"Dialog.submit();\"><span class=\"button-text\">Log In</span></div></td></tr></table></div><script>Dialog.makeElementSubmit('email_input');Dialog.makeElementSubmit('password_input'); Dialog.makeElementSubmit('submit_button');</script></form></td></tr></table></div></div><div class=\"trailer\"><div class=\"formpage-footer\"><a class=\"footer-link\" href=\"http://asana.com\" target=\"_blank\">About Asana</a>|<a class=\"footer-link\" href=\"http://asana.com/blog\" target=\"_blank\">Blog</a>|<a class=\"footer-link\" href=\"http://asana.com/jobs\" target=\"_blank\">Jobs</a>|<a class=\"footer-link\" href=\"http://asana.com/help\" target=\"_blank\">Help</a>|<a class=\"footer-link\" href=\"http://asana.com/terms\" target=\"_blank\">Terms</a></div><div class=\"login-link\"><!-- DEBUG-TAG: login-signup -->Don&#146;t have an account? <a class=\"footer-link\" href=\"http://www.asana.com/?utm_source=unknown&amp;utm_campaign=app.asana.com\">Sign Up</a></div><!-- Commenting out FBConnect for now.<td class=\"facebook-label\">Or,&nbsp;Connect&nbsp;with&nbsp;Facebook</td><td class=\"facebook-button\"><a href=\"#\" onclick=\"Dialog.Login.loginUsingFacebook();\"><img src=\"/-/static/luna/browser/images/connect.gif\" alt=\"Facebook Connect\" /></a>--></div></div></div></div></div></div><script>var initial_focused_element = 'email_input';</script><script>\n  document.getElementById(initial_focused_element).focus();\n\n</script>\n<div id=\"debug_page_load_marker\" name=\"login\" style=\"display:none\"></div>\n</body>\n</html>"

1 个答案:

答案 0 :(得分:1)

法拉第将/tasks解释为服务器上的绝对路径,因此您应该看到

$ ASANA_URL="https://app.asana.com/api/1.0/" ruby test.rb I, [2014-08-28T11:18:31.830026 #49279] INFO -- : get https://app.asana.com/tasks?project=14790966979027

请注意,网址错误 - 应为https://app.asana.com/api/1.0/tasks?project=14790966979027。如果您替换conn.get "/tasks" =&gt;第8行conn.get "tasks"

总是很好检查调试输出以确保URL与您使用curl发送的内容匹配: - )