2015-05-22 53 views
0

我是Python的新手,因此忍受着我... 嘗試對Instagram API進行簡單的oAuth調用。註冊您的應用程序後,你會得到您的客戶端ID,客戶端密鑰等,在OAuth過程的第一步是將用戶定向到該授權網址:使用Python請求自定義GET響應模塊

https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code 

當我在瀏覽器中加載這個URL與我的客戶ID和重定向URL,下面的URL出現在瀏覽器(例如):

http://instagrram.geometryfletch.com/home.html?code=956237827314ee22092384984938 

我的問題是,我怎麼能複製使用請求模塊瀏覽器會發生什麼?

,當我嘗試以下方法:

>>> import requests 
>>> b = requests.get('https://api.instagram.com/oauth/authorize/?client_id=c918883453360349850498&redirect_uri=http://instagrram.myredirect.com/home.html&response_type=code') 
>>> b.text 

我得到的回覆是這種「garbbled」響應(我知道這是不是真的grabbled,請在做什麼,我告訴它,返回一些適當的):

u'<!DOCTYPE html>\n<!--[if lt IE 7]>  <html lang="en" class="no-js lt-ie9 lt-ie8 lt-ie7 not-logged-in "> <![endif]-->\n<!--[if IE 7]>   <html lang="en" class="no-js lt-ie9 lt-ie8 not-logged-in "> <![endif]-->\n<!--[if IE 8]>   <html lang="en" class="no-js lt-ie9 not-logged-in "> <![endif]-->\n<!--[if gt IE 8]><!--> 
<html lang="en" class="no-js not-logged-in "> <!--<![endif]-->\n 
<head>\n 
    <meta charset="utf-8"> 
    \n 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    \n\n <title>Log in &mdash; Instagram</title>\n\n 
    <script type="text/javascript">\ 
    n 
    WebFontConfig = { 
    \ 
    n 
    custom: {\n 
     families: [\'proxima-nova:n4,n7\'],\n  urls: [\'//instagramstatic-a.akamaihd.net/bluebar/660508e/cache/styles/fonts.css\']\n }\n };\n</script> 
    \n 
    <script src="//instagramstatic-a.akamaihd.net/bluebar/660508e/scripts/webfont.js" type="text/javascript" 
      async></script> 
    \n\n \n \n 
    <meta name="robots" content="noimageindex"> 
    \n \n 
    <meta name="apple-mobile-web-app-capable" content="yes"> 
    \n 
    <meta name="apple-mobile-web-app-status-bar-style" content="black"> 
    \n\n\n \n 
    <meta id="viewport" name="viewport" 
      content="width=device-width, user-scalable=no, initial-scale=1, minimum-scale=1, maximum-scale=1"> 
    \n\n\n 
    <script type="text/javascript">\ 
    n(function() {\n 
     var docElement = document.documentElement;\n 
     var classRE = new RegExp(\'(^|\\\\s)no-js(\\\\s|$)\');\n   var className = docElement.className;\n   docElement.className = className.replace(classRE, \'$1js$2\');\n  })();\n  </script> 
    \n\n \n\n \n \n \n 
    <link rel="Shortcut Icon" type="image/x-icon" 
      href="//instagramstatic-a.akamaihd.net/bluebar/660508e/images/ico/favicon.ico"> 
    \n \n \n 
    <link rel="apple-touch-icon-precomposed" 
      href="//instagramstatic-a.akamaihd.net/bluebar/660508e/images/ico/apple-touch-icon-precomposed.png"> 
    \n 
    <link rel="apple-touch-icon-precomposed" sizes="72x72" 
      href="//instagramstatic-a.akamaihd.net/bluebar/660508e/images/ico/apple-touch-icon-72x72-precomposed.png"> 
    \n 
    <link rel="apple-touch-icon-precomposed" sizes="114x114" 
      href="//instagramstatic-a.akamaihd.net/bluebar/660508e/images/ico/apple-touch-icon-114x114-precomposed.png"> 
    \n 
    <link rel="apple-touch-icon-precomposed" sizes="144x144" 
      href="//instagramstatic-a.akamaihd.net/bluebar/660508e/images/ico/apple-touch-icon-144x144-precomposed.png"> 
    \n \n \n 
    <link href="//instagramstatic-a.akamaihd.net/bluebar/660508e/cache/styles/distillery/dialog-main.css" 
      type="text/css" rel="stylesheet"></link> 
    \n 
    <!--[if lt IE 9]>\n <style>\n  .dialog-outer {\n   min-height: 0;\n  }\n </style>\n  
    <![endif]-->\n\n \n 
    <script src="//instagramstatic-a.akamaihd.net/bluebar/660508e/scripts/jquery.js" type="text/javascript"></script> 
    \n 
    <script src="//instagramstatic-a.akamaihd.net/bluebar/660508e/scripts/bluebar.js" type="text/javascript"></script> 
    \n 
    <script type="text/javascript">\ 
    n 
    $(document).ready(function() {\n 
     $("#id_username").focus();\n 
     setTimeout(function() {\n 
      document.getElementById(\'viewport\').setAttribute(\'content\', \'width=\'+ window.innerWidth + \', user-scalable=no\');\n  }, 5);\n });\n</script> 
    \n\n\n 
</head> 
\n 
<body class="p-dialog oauth-login">\n \n \n 
<div class="root">\n \n 
    <section class="dialog-outer">\n 
     <div class="dialog">\n 
      <header>\n <h1 class="logo">Instagram</h1>\n \n</header> 
      \n 
      <div class="dialog-main">\n \n\n\n\n\n\n\n 
       <form method="POST" id="login-form" class="adjacent" 
         action="/accounts/login/?force_classic_login=&amp;next=/oauth/authorize/?client_id=c91888345336494ab7ea7046427ca23e%26redirect_uri=http://instagrram.geometryfletch.com/home.html%26response_type=code"> 
        \n <input type="hidden" name="csrfmiddlewaretoken" .......... 

,但我怎麼能得到請求返回時只是簡單的code:code=956237827314ee22092384984938時加載的URL到瀏覽器?

+0

您在此問題中提供的任何憑據/用戶信息是否更好地保密? –

+0

你爲什麼要這樣做,而不是使用完美的[Instagram Python庫](https://github.com/instagram/python-instagram)? –

+0

@ Jan-PhilipGehrcke問題中沒有任何信息是我的實際憑證信息,我只是爲了發佈此問題而編制了這些信用。 – AdjunctProfessorFalcon

回答

1

爲了生產目的,您不應該重新實施oauth。請看https://pypi.python.org/pypi/oauthlib這是一個用於執行oauth認證邏輯的已建立的庫。如果你想堅持requests,那麼也有https://github.com/requests/requests-oauthlib。除此之外,關於你的問題

我的問題是,我怎麼能複製使用 請求模塊瀏覽器會發生什麼?

這不是微不足道的。首先,使用curl或瀏覽器插件來調試/重建協議流。然後第二步是使用requests重建相同的流程。

例:當通過GET訪問你在你的問題中提到的第一個網址,服務器以302 redirection,其目標在響應Location場給出迴應。該響應還通過Set-Cookie標題字段設置了一個cookie。所有這些都很重要。

+0

感謝您提供豐富的回覆,非常感謝。需要說明的是,在請求中的瀏覽器中加載該URL的方式並不相同。我只能用cURL來做到這一點嗎? – AdjunctProfessorFalcon

+0

瀏覽器在比請求更高的級別上工作。你不能告訴請求「請像Firefox一樣行事」。但是,請求肯定會提供您自己實現瀏覽器所需的所有工具。您只需理解瀏覽器在後臺執行的操作通常比發出HTTP GET請求和「顯示」響應要複雜得多。響應通過一切手段進行評估,並根據響應的具體情況採取行動,包括所有標題字段。 –

+0

您應該安裝一個可視化HTTP協議流的瀏覽器插件。然後訪問一個簡單的網站,仔細看看發生了什麼。然後登錄Facebook並查看發生了什麼。這將是非常複雜的。正如我所說的,所有這些*都可以使用請求(或任何其他HTTP庫)重新實現,但它可能需要很多工作。 –