{"id":452,"date":"2015-05-06T00:16:39","date_gmt":"2015-05-05T15:16:39","guid":{"rendered":"http:\/\/www.typea.info\/blog\/index.php\/2015\/05\/06\/django_python-twitter_oauth_twitter_api\/"},"modified":"2015-05-06T00:16:39","modified_gmt":"2015-05-05T15:16:39","slug":"django_python-twitter_oauth_twitter_api","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2015\/05\/06\/django_python-twitter_oauth_twitter_api\/","title":{"rendered":"Django \u304b\u3089 python-twitter \u3067 OAuth\u8a8d\u8a3c\u3057\u3066 Twitter API\u3092\u4f7f\u3063\u3066\u307f\u308b"},"content":{"rendered":"<p>Django\u304b\u3089Twitter\u30a2\u30ab\u30a6\u30f3\u30c8\u306bOAuth\u8a8d\u8a3c\u3057\u3066Twitter API \u3092\u5229\u7528\u3057\u3088\u3046\u3068\u601d\u3046\u3002<\/p>\n<p><a href=\"https:\/\/github.com\/bear\/python-twitter\">python-twitter<\/a> \u3092\u5229\u7528\u3057\u3066\u5bb9\u6613\u306b\u5b9f\u73fe\u3002\u3002\u3002\u3068\u601d\u3044\u304d\u3084\u3001\u82e5\u5e72\u306f\u307e\u3063\u305f\u306e\u3067\u3001\u30e1\u30e2\u3057\u3066\u304a\u304f\u3002<\/p>\n<p>\u3084\u308a\u305f\u3044\u3053\u3068\u306f\u3001\u5229\u7528\u8005\u306e\u8a8d\u8a3c\u3068\u3001\u5229\u7528\u8005\u306e\u30ea\u30bd\u30fc\u30b9\u3078\u306eTwitterAPI\u7d4c\u7531\u3067\u306e\u30a2\u30af\u30bb\u30b9\u3002<\/p>\n<h2><a href=\"http:\/\/www.atmarkit.co.jp\/fsecurity\/rensai\/digid01\/02.html\">1.OAuth\u306e\u57fa\u672c\u7684\u306a\u6d41\u308c<\/a><\/h2>\n<p>\u307e\u305a\u306f\u3001\u5229\u7528\u8005\u306bTwitter\u306e\u30e6\u30fc\u30b6\u30fc\u8a8d\u8a3c\u3092\u3055\u305b\u305f\u3044\u3002Twitter\u3067\u306f\u3001\u8a8d\u8a3c\u306bOAuth\u3092\u5229\u7528\u3057\u3066\u3044\u308b\u3002OAuth\u306b\u3088\u308b\u8a8d\u8a3c\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u306f\u3001\u4ee5\u4e0b\u306e\u30b5\u30a4\u30c8\u304c\u308f\u304b\u308a\u3084\u3059\u3044\u3002<\/p>\n<p><a title=\"http:\/\/www.atmarkit.co.jp\/fsecurity\/rensai\/digid01\/02.html\" href=\"http:\/\/www.atmarkit.co.jp\/fsecurity\/rensai\/digid01\/02.html\">http:\/\/www.atmarkit.co.jp\/fsecurity\/rensai\/digid01\/02.html<\/a><\/p>\n<p>\u4e0b\u56f3\u3067\u3044\u3046\u3068\u3001Resource Owner \u304c\u3001Twitter\u306e\u30e6\u30fc\u30b6\u30fc\u3067\u3042\u308a\u3001\u958b\u767a\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u5229\u7528\u8005\u3002OAuth Client \u304c\u3001\u958b\u767a\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3001OAuth Server \u304c Twitter \u3068\u306a\u308b\u3002<\/p>\n<p>\u8a8d\u8a3c\u307e\u3067\u306e\u6d41\u308c\u3092\u78ba\u8a8d\u3002<\/p>\n<p><a href=\"http:\/\/www.amazon.co.jp\/gp\/product\/B007CNFP3A\/ref=as_li_ss_il?ie=UTF8&amp;camp=247&amp;creative=7399&amp;creativeASIN=B007CNFP3A&amp;linkCode=as2&amp;tag=typea09-22\"><img decoding=\"async\" border=\"0\" src=\"http:\/\/ws-fe.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=B007CNFP3A&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=JP&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=typea09-22\"><\/a><img loading=\"lazy\" decoding=\"async\" style=\"border-top-style: none !important; border-bottom-style: none !important; border-right-style: none !important; margin: 0px; border-left-style: none !important\" border=\"0\" alt=\"\" src=\"http:\/\/ir-jp.amazon-adsystem.com\/e\/ir?t=typea09-22&amp;l=as2&amp;o=9&amp;a=B007CNFP3A\" width=\"1\" height=\"1\">  <\/p>\n<ol>\n<li><strong>&lt;\u4e8b\u524d&gt;:<\/strong> OAuth \u30b5\u30fc\u30d0\u30fc\u306b\u3001\u81ea\u8eab\u306e\u60c5\u5831\u3092\u767b\u9332\n<li><strong>&lt;\u30d5\u30ed\u30fc\u306e\u958b\u59cb&gt; Initiate<\/strong>: OAuth \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u4e0a\u306b\u3001\u300cFacebook ID\u300d\u3067\u30ed\u30b0\u30a4\u30f3\u306a\u3069\u306e\u30dc\u30bf\u30f3\u304c\u3042\u308a\u3001\u30e6\u30fc\u30b6\u30fc\u304c\u305d\u308c\u3092\u62bc\u4e0b\u3059\u308b\u3053\u3068\u3067\u958b\u59cb\u3002\n<li><strong>&lt;\u8a8d\u53ef\u306e\u30ea\u30af\u30a8\u30b9\u30c8&gt; Authorization Request<\/strong>: OAuth \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306f\u3001OAuth\u30b5\u30fc\u30d0\u30fc\u3078\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3002(OAuth\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306e\u8b58\u5225\u60c5\u5831\u3092\u4ed8\u4e0e)\n<li><strong>&lt;\u30e6\u30fc\u30b6\u30fc\u8a8d\u8a3c\u3068\u30a2\u30af\u30bb\u30b9\u6a29\u4ed8\u4e0e&gt; Authenticate User &amp; Get Approval<\/strong>: OAuth \u30b5\u30fc\u30d0\u30fc\u306f\u30e6\u30fc\u30b6\u30fc\u3092\u8a8d\u8a3c\u3057\u3001OAuth \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3078\u30a2\u30af\u30bb\u30b9\u6a29\u3092\u4ed8\u4e0e\u3059\u308b\u304b\u3092\u78ba\u8a8d\u3002\n<li><strong>&lt;\u8a8d\u53ef\u30ec\u30b9\u30dd\u30f3\u30b9&gt; Authrization Response<\/strong>: OAuth \u30b5\u30fc\u30d0\u30fc\u306f\u3001OAuth\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3078\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3002\u30ea\u30c0\u30a4\u30ec\u30af\u30c8URL\u306b\u306f\u30a2\u30af\u30bb\u30b9\u6a29\u9650\u4ed8\u4e0e\u3092\u793a\u3059\u30c8\u30fc\u30af\u30f3\u3092\u542b\u3080\u3002\n<li><strong>&lt;\u30a2\u30af\u30bb\u30b9\u30c8\u30fc\u30af\u30f3\u3068\u306e\u4ea4\u63db&gt; Obtain Access Token<\/strong>: OAuth\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306f\u53d7\u3051\u53d6\u3063\u305f\u30c8\u30fc\u30af\u30f3\u3092Access Token(\u30a2\u30af\u30bb\u30b9\u30c8\u30fc\u30af\u30f3 \u5f8c\u306bAPI\u30a2\u30af\u30bb\u30b9\u3092\u5229\u7528\u3059\u308b\u3068\u304d\u306b\u7528\u3044\u308b)\u3068\u4ea4\u63db\u3002<\/li>\n<\/ol>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Django--python-twitter-_96F5\/zu02.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"zu02\" style=\"border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px\" border=\"0\" alt=\"zu02\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Django--python-twitter-_96F5\/zu02_thumb.jpg\" width=\"390\" height=\"340\"><\/a><\/p>\n<h2>2.Twitter\u3078\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u767b\u9332<\/h2>\n<p><a title=\"https:\/\/apps.twitter.com\/\" href=\"https:\/\/apps.twitter.com\/\">https:\/\/apps.twitter.com\/<\/a><\/p>\n<p>\u307e\u305a\u3001\u4e8b\u524d\u51e6\u7406\u3068\u3057\u3066\u3001Twitter\u306b\u958b\u767a\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u767b\u9332\u3092\u884c\u3046\u5fc5\u8981\u304c\u3042\u308a\u3001\u4e0a\u8a18 &lt;\u8a8d\u53ef\u30ec\u30b9\u30dd\u30f3\u30b9&gt;\u3067\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3059\u308b\u5148\u306eURL\u3092 Callback URL\u306b\u767b\u9332\u3059\u308b\u3002<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Django--python-twitter-_96F5\/twitter_app_callback.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"twitter_app_callback\" style=\"border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px\" border=\"0\" alt=\"twitter_app_callback\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Django--python-twitter-_96F5\/twitter_app_callback_thumb.jpg\" width=\"410\" height=\"134\"><\/a><\/p>\n<h2>3.python-twitter \u306b\u3088\u308b\u30b5\u30f3\u30d7\u30eb<\/h2>\n<p>\u4f8b\u5916\u51e6\u7406\u306a\u3069\u3092\u53d6\u308a\u9664\u3044\u3066\u5f15\u7528\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308b\u3002<\/p>\n<pre class=\"prettyprint linenums\"># 1.\u8a8d\u53ef\u30ea\u30af\u30a8\u30b9\u30c8\u3068\u30ea\u30c0\u30a4\u30ec\u30af\u30c8URL\u306e\u53d6\u5f97\r\nresp = oauth_client.fetch_request_token(REQUEST_TOKEN_URL)\r\nurl = oauth_client.authorization_url(AUTHORIZATION_URL)\r\n\r\n# 2.\u6307\u5b9aURL\u3067OAuth\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3078\u6a29\u9650\u306e\u4ed8\u4e0e\r\nwebbrowser.open(url)\r\n\r\n# 3.\u4e0a\u8a18 1.\u3067\u53d6\u5f97\u3057\u305f\u7d50\u679c\u304b\u3089\u3001oauth_token\u3001oauth_token_secret \u3092\u53d6\u5f97\r\noauth_client = OAuth1Session(consumer_key, client_secret=consumer_secret,\r\n                             resource_owner_key=resp.get('oauth_token'),\r\n                             resource_owner_secret=resp.get('oauth_token_secret'),\r\n                             verifier=pincode)<\/pre>\n<p>\u306f\u307e\u3063\u305f\u306e\u306f\u3001\u4e0a\u8a182. \u3068 3. \u306e\u9593\u3002<\/p>\n<p>\u5b9f\u969b\u306b\u5b9f\u88c5\u3059\u308b\u3068\u304d\u306b\u306f\u3001<\/p>\n<ol>\n<li>\u958b\u767a\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u304c\u3001\u4e0a\u8a181.\u3092\u5b9f\u884c\u3057\u3001\u53d6\u5f97\u3057\u305f URL\u306b\u30e6\u30fc\u30b6\u30fc\u3092\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\n<li>\u30e6\u30fc\u30b6\u30fc\u306f\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u5148\u3067\u3001\u958b\u767a\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306b\u6a29\u9650\u3092\u4e0e\u3048\u308b\u3068\u3001OAuth\u30b5\u30fc\u30d0\u30fc\u3067\u3042\u308bTwitter\u304c\u3001\u4e0a\u8a18\u3067\u8a2d\u5b9a\u3057\u305fCallback URL \u306e\u30af\u30a8\u30ea\u30d1\u30e9\u30e1\u30fc\u30bf\u306b oauth_token \u3092\u4ed8\u4e0e\u3057\u3066(oauth_token_secret \u306f\u4ed8\u4e0e\u3055\u308c\u306a\u3044) \u30ea\u30c0\u30a4\u30ec\u30af\u30c8\n<li>\u958b\u767a\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3055\u308c\u305fURL\u304b\u3089\u3000oauth_token \u3092\u53d6\u308a\u51fa\u3057\u3066\u51e6\u7406<\/li>\n<\/ol>\n<p>\u3068\u3044\u3046\u6d41\u308c\u306b\u306a\u308b\u306e\u3060\u304c\u3001Twitter\u304b\u3089\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3055\u308c\u308bURL\u306e\u30af\u30a8\u30ea\u30d1\u30e9\u30e1\u30fc\u30bf\u306b\u306f\u3001oauth_token_secret \u304c\u542b\u307e\u308c\u306a\u3044\u30021.\u306e\u51e6\u7406\u3067\u3001oauth_toke_secret\u306f\u53d6\u5f97\u3067\u304d\u308b\u306e\u3060\u304c\u3002\u3002\u3002<\/p>\n<p>\u305d\u3046\u3044\u3046\u3082\u306e\u306a\u306e\u304b\uff1f\u4f55\u304b\u9055\u548c\u611f\u304c\u3042\u308b\u3002\u3002\u3002<\/p>\n<p>\u3068\u3001\u601d\u3063\u3066\u8a66\u884c\u932f\u8aa4\u3057\u3066\u3044\u305f\u306e\u3060\u304c\u3001<\/p>\n<p><a title=\"http:\/\/pika-shi.hatenablog.com\/entry\/20120210\/1328866010\" href=\"http:\/\/pika-shi.hatenablog.com\/entry\/20120210\/1328866010\">http:\/\/pika-shi.hatenablog.com\/entry\/20120210\/1328866010<\/a><\/p>\n<blockquote>\n<p>\u3061\u306a\u307f\u306b\uff0coauth\u8a8d\u8a3c\u3067\u500b\u4eba\u7684\u306b\u3081\u3093\u3069\u304f\u3055\u304b\u3063\u305f\u70b9\u306f\uff0c\u8a8d\u8a3c\u524d\u306b\u53d6\u5f97\u3057\u305fRequest token\u3068Request token secret\u304c\uff0c\u8a8d\u8a3c\u5f8c\u306b\u3082\u5fc5\u8981\u3068\u306a\u308b\u70b9\uff0e <\/p>\n<p>\u3059\u306a\u308f\u3061\uff0cWeb\u30da\u30fc\u30b8\u306e\u9077\u79fb\u306e\u305f\u3081\uff0c\u30bb\u30c3\u30b7\u30e7\u30f3\u306e\u7ba1\u7406\u304c\u5fc5\u8981\u306b\u306a\u308b\uff0e<\/p>\n<\/blockquote>\n<p>\u305d\u3046\u3044\u3046\u3082\u306e\u307f\u305f\u3044\u3060\u3002\u306a\u30fc\u3093\u3060\u3002<\/p>\n<h2>4.\u3068\u3044\u3046\u3053\u3068\u3067\u5b9f\u88c5\u3057\u3066\u307f\u308b<\/h2>\n<h3>4.1 \u30d5\u30ed\u30fc\u958b\u59cb<\/h3>\n<p>\u30a2\u30d7\u30ea\u306b\u3001Twitter\u3067\u30ed\u30b0\u30a4\u30f3\u30dc\u30bf\u30f3\u3092\u7f6e\u3044\u3066\u307f\u307e\u3059\u3002<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Django--python-twitter-_96F5\/login_by_twitter.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"login_by_twitter\" style=\"border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px\" border=\"0\" alt=\"login_by_twitter\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Django--python-twitter-_96F5\/login_by_twitter_thumb.jpg\" width=\"341\" height=\"283\"><\/a><\/p>\n<h3>4.2 \u8a8d\u53ef\u306e\u30ea\u30af\u30a8\u30b9\u30c8<\/h3>\n<h4>(1)&nbsp; \u30c8\u30fc\u30af\u30f3\u3092\u4fdd\u6301\u3059\u308b\u305f\u3081\u306e\u30e2\u30c7\u30eb\u3092\u7528\u610f<\/h4>\n<pre class=\"prettyprint linenums\">from django.db import models\r\n\r\nclass OAuthTokenTemp(models.Model):\r\n    '''\r\n    '''\r\n    oauth_token = models.CharField(max_length=255, db_index=True, unique=True)\r\n    oauth_token_secret = models.CharField(max_length=255, db_index=True, unique=True)\r\n<\/pre>\n<h4>(2) \u5b9f\u88c5<\/h4>\n<ul>\n<li>Twitter\u3067\u30ed\u30b0\u30a4\u30f3\u30dc\u30bf\u30f3\u304c\u304a\u3055\u308c\u305f\u3089\u3001\u4ee5\u4e0b\u306e\u95a2\u6570\u3092\u547c\u3073\u51fa\u3057\u3066\u3001\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u5148\u306eURL\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002\n<li>CONSUMER_KEY \u3068 CONSUMER_SECRET \u306f\u3001<a href=\"https:\/\/apps.twitter.com\/\">Twitter\u30a2\u30d7\u30ea\u3092\u767b\u9332<\/a>\u3059\u308b\u3068\u767a\u884c\u3055\u308c\u308b\u5024\u3092\u4f7f\u7528\u3002\n<li>\u4f8b\u5916\u51e6\u7406\u3068\u304b\u306f\u3001\u3068\u308a\u3042\u3048\u305a\u7701\u3044\u3066\u3044\u308b\u305f\u3081\u9069\u5b9c\u304a\u3053\u306a\u3046\u3002\n<li>\u53d6\u5f97\u3057\u305foauth_token,oauth_token_secret \u3092DB\u306b\u4fdd\u6301\u3002\n<li>\u53d6\u5f97\u3057\u305fURL\u3078\u4f55\u3089\u304b\u306e\u65b9\u6cd5\u3067\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3002<\/li>\n<\/ul>\n<pre class=\"prettyprint linenums\">import twitter\r\nfrom requests_oauthlib import OAuth1Session\r\n\r\n_CONSUMER_KEY = '*****************'\r\n_CONSUMER_SECRET = '*****************'\r\n\r\nREQUEST_TOKEN_URL = 'https:\/\/api.twitter.com\/oauth\/request_token'\r\nAUTHORIZATION_URL = 'https:\/\/api.twitter.com\/oauth\/authorize'\r\n\r\ndef authorization_request():\r\n    '''\r\n    \u8a8d\u53ef\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u884c\u3044Twitter\u8a8d\u8a3c\u30da\u30fc\u30b8\u3078\u306eURL\u3092\u53d6\u5f97\r\n    '''\r\n    oauth_client = OAuth1Session(_CONSUMER_KEY, client_secret=_CONSUMER_SECRET)\r\n    try:\r\n        resp = oauth_client.fetch_request_token(REQUEST_TOKEN_URL)\r\n        \r\n        oauth = OAuthTokenTemp(oauth_token=resp.get('oauth_token'),\r\n                             oauth_token_secret=resp.get('oauth_token_secret'))\r\n        \r\n        oauth.save()\r\n    except ValueError, e:\r\n        print e\r\n        return\r\n    return  oauth_client.authorization_url(AUTHORIZATION_URL)\r\n<\/pre>\n<h3>4.3 \u30e6\u30fc\u30b6\u30fc\u8a8d\u8a3c\u3068\u30a2\u30af\u30bb\u30b9\u6a29\u4ed8\u4e0e<\/h3>\n<p>Twitter \u5074\u3067\u30e6\u30fc\u30b6\u30fc\u8a8d\u8a3c\u3068\u3001\u30a2\u30d7\u30ea\u306b\u5bfe\u3059\u308b\u6a29\u9650\u4ed8\u4e0e\u3092\u884c\u3063\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Django--python-twitter-_96F5\/twitter_user_approval.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"twitter_user_approval\" style=\"border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px\" border=\"0\" alt=\"twitter_user_approval\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Django--python-twitter-_96F5\/twitter_user_approval_thumb.jpg\" width=\"426\" height=\"287\"><\/a><\/p>\n<h3>4.4 \u8a8d\u53ef\u30ec\u30b9\u30dd\u30f3\u30b9<\/h3>\n<p>\u9023\u643a\u30a2\u30d7\u30ea\u3092\u8a8d\u8a3c\u3059\u308b\u3068\u305f\u3068\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a URL\u306b\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3055\u308c\u307e\u3059\u3002<\/p>\n<p><a href=\"http:\/\/test.phraseit.info?oauth_token=5wFYbG7CyxNyJYkcZ6Edurf7BPJ8iYzm&amp;oauth_verifier=vT0pBegUKQJyMU7Q0ErLSRU6ZLXHEe0D\">http:\/\/test.phraseit.info?oauth_token=5wFYbG7CyxNyJYkcZ6Edurf7BPJ8iYzm&amp;oauth_verifier=vT0pBegUKQJyMU7Q0ErLSRU6ZLXHEe0D<\/a><\/p>\n<ul>\n<li>\u30af\u30a8\u30ea\u30d1\u30e9\u30e1\u30fc\u30bf\u306b oauth_token \u304c\u542b\u307e\u308c\u308b\u306e\u3067\u3001\u5148\u307b\u3069\u4fdd\u5b58\u3057\u305fDB\u3092\u691c\u7d22\u3057\u3001oauth_token_secret\u3092\u53d6\u5f97\u3059\u308b\u3002 <a href=\"http:\/\/www.amazon.co.jp\/gp\/product\/477414732X\/ref=as_li_ss_il?ie=UTF8&amp;camp=247&amp;creative=7399&amp;creativeASIN=477414732X&amp;linkCode=as2&amp;tag=typea09-22\"><img decoding=\"async\" border=\"0\" src=\"http:\/\/ws-fe.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=477414732X&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=JP&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=typea09-22\"><\/a><img loading=\"lazy\" decoding=\"async\" style=\"border-top-style: none !important; border-bottom-style: none !important; border-right-style: none !important; margin: 0px; border-left-style: none !important\" border=\"0\" alt=\"\" src=\"http:\/\/ir-jp.amazon-adsystem.com\/e\/ir?t=typea09-22&amp;l=as2&amp;o=9&amp;a=477414732X\" width=\"1\" height=\"1\">\n<li>\u540c\u3058\u304f\u30af\u30a8\u30ea\u30d1\u30e9\u30e1\u30fc\u30bf\u306eoauth_verifire \u3082\u542b\u3081\u3001\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u958b\u59cb\u3059\u308b\u30d1\u30e9\u30e1\u30fc\u30bf\u304c\u305d\u308d\u3046\u306e\u3067\u3001\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u958b\u59cb\u3057\u3001\u30a2\u30af\u30bb\u30b9\u30c8\u30fc\u30af\u30f3\u3092\u53d6\u5f97\n<li>\u3053\u308c\u3067API\u304c\u5229\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u305f\u3081\u3001\u30e6\u30fc\u30b6\u30fc\u60c5\u5831(\u4e0b\u4f8b tw_user)\u3092\u53d6\u5f97\u3057\u3066\u307f\u308b\u3002<\/li>\n<\/ul>\n<pre class=\"prettyprint linenums\">from django.http import HttpResponse\r\nimport twitter\r\nfrom requests_oauthlib import OAuth1Session\r\n\r\n_CONSUMER_KEY = '****************************'\r\n_CONSUMER_SECRET = ''****************************'\r\n\r\nACCESS_TOKEN_URL = 'https:\/\/api.twitter.com\/oauth\/access_token'\r\n\r\ndef twitter_auth(request):\r\n    '''\r\n    Twitter\u8a8d\u8a3c\u3092\u884c\u3046\r\n    '''\r\n    oauth_token = request.GET[\"oauth_token\"]\r\n    oauth_verifier = request.GET[\"oauth_verifier\"]\r\n\r\n    oauth = OAuthTokenTemp.objects.get(oauth_token=oauth_token)\r\n    oauth_token_secret =  oauth.oauth_token_secret\r\n    \r\n    oauth_client = OAuth1Session(_CONSUMER_KEY, client_secret=_CONSUMER_SECRET,\r\n                                 resource_owner_key=oauth_token,\r\n                                 resource_owner_secret=oauth_token_secret,\r\n                                 verifier=oauth_verifier)\r\n    try:\r\n        resp = oauth_client.fetch_access_token(ACCESS_TOKEN_URL)\r\n    except ValueError, e:\r\n        return\r\n                                 \r\n    api = twitter.Api(consumer_key=_CONSUMER_KEY,\r\n                      consumer_secret=_CONSUMER_SECRET,\r\n                      access_token_key=resp.get('oauth_token'),\r\n                      access_token_secret=resp.get('oauth_token_secret'))\r\n\r\n    tw_user = api.VerifyCredentials()\r\n\u3000\u3000\u3000oauth.delete()\r\n          :<\/pre>\n<p><a title=\"https:\/\/dev.twitter.com\/oauth\/overview\/faq\" href=\"https:\/\/dev.twitter.com\/oauth\/overview\/faq\">https:\/\/dev.twitter.com\/oauth\/overview\/faq<\/a><\/p>\n<blockquote>\n<p>How long does an access token last?<br \/>We do not currently expire access tokens.<br \/>Your access token will be invalid if a user explicitly rejects <br \/>your application from their settings or if a Twitter admin suspends your application.<br \/>If your application is suspended there will be a note on your application page <br \/>saying that it has been suspended.<\/p>\n<\/blockquote>\n<p>\u3053\u3053\u3067\u3001\u53d6\u5f97\u3057\u305f\u30a2\u30af\u30bb\u30b9\u30c8\u30fc\u30af\u30f3\u306f\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u304c\u7834\u68c4\u3057\u306a\u3051\u308c\u3070\u3001\u518d\u5229\u7528\u3067\u304d\u308b\u3088\u3046\u306a\u306e\u3067\u3001\u958b\u767a\u30a2\u30d7\u30ea\u306e\u30e6\u30fc\u30b6\u30fc\u60c5\u5831\u306b\u4fdd\u6301\u3057\u3066\u304a\u304d\u3001\u518d\u5ea6API\u3092\u547c\u3073\u51fa\u3059\u3068\u304d\u306b\u5229\u7528\u3059\u308b\u3002<\/p>\n<h3>4.5 \u6210\u529f\uff01<\/h3>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4e0a\u8a18\u624b\u9806\u306b\u3066\u3001Twitter\u306e\u30e6\u30fc\u30b6\u30fc\u60c5\u5831\u304b\u3089\u5199\u771f\u3068\u540d\u524d\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u3001\u753b\u9762\u306b\u8868\u793a\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n<p>\u3081\u3067\u305f\u3057\u3081\u3067\u305f\u3057\u3002<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Django--python-twitter-_96F5\/twitter_access_success.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"twitter_access_success\" style=\"border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px\" border=\"0\" alt=\"twitter_access_success\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Django--python-twitter-_96F5\/twitter_access_success_thumb.jpg\" width=\"354\" height=\"132\"><\/a><\/p>\n<p><iframe style=\"height: 240px; width: 120px\" marginheight=\"0\" src=\"http:\/\/rcm-fe.amazon-adsystem.com\/e\/cm?lt1=_blank&amp;bc1=FFFFFF&amp;IS2=1&amp;bg1=FFFFFF&amp;fc1=000000&amp;lc1=0000FF&amp;t=typea09-22&amp;o=9&amp;p=8&amp;l=as4&amp;m=amazon&amp;f=ifr&amp;ref=ss_til&amp;asins=4048686291\" frameborder=\"0\" marginwidth=\"0\" scrolling=\"no\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Django\u304b\u3089Twitter\u30a2\u30ab\u30a6\u30f3\u30c8\u306bOAuth\u8a8d\u8a3c\u3057\u3066Twitter API \u3092\u5229\u7528\u3057\u3088\u3046\u3068\u601d\u3046\u3002 python-twitter \u3092\u5229\u7528\u3057\u3066\u5bb9\u6613\u306b\u5b9f\u73fe\u3002\u3002\u3002\u3068\u601d\u3044\u304d\u3084\u3001\u82e5\u5e72\u306f\u307e\u3063\u305f\u306e\u3067\u3001\u30e1\u30e2\u3057\u3066\u304a\u304f\u3002 \u3084\u308a\u305f\u3044\u3053\u3068 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[29,81,20,82],"tags":[],"class_list":["post-452","post","type-post","status-publish","format-standard","hentry","category-django","category-oauth","category-python","category-twitter"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/452","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=452"}],"version-history":[{"count":0,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/452\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}