与mod_wsgi + Apache一起使用时,oauth2.request的IOError

时间:2016-07-07 07:03:18

标签: python apache flask mod-wsgi

对于第三方API,我使用的是oauth2模块,我有以下代码: 当我使用python run.py但在使用IOError配置时拒绝apache +mod_wsgi权限时,哪个效果很好。

这是错误:

ERROR: Traceback (most recent call last):
   File "/home/trex/workspace/scripts/flaskapp.py", line 32, in getTwitter_acc_audiences
     _, content = client.request(uri=uri, method="GET")
   File "build/bdist.linux-x86_64/egg/oauth2/__init__.py", line 687, in request
     connection_type=connection_type)
   File "/home/trex/workspace/myenv/lib/python2.7/site-packages/httplib2-0.9.2-py2.7.egg/httplib2/__init__.py", line 1609, in request
     (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
   File "/home/trex/workspace/myenv/lib/python2.7/site-packages/httplib2-0.9.2-py2.7.egg/httplib2/__init__.py", line 1351, in _request
     (response, content) = self._conn_request(conn, request_uri, method, body, headers)
   File "/home/trex/workspace/myenv/lib/python2.7/site-packages/httplib2-0.9.2-py2.7.egg/httplib2/__init__.py", line 1272, in _conn_request
     conn.connect()
   File "/home/trex/workspace/myenv/lib/python2.7/site-packages/httplib2-0.9.2-py2.7.egg/httplib2/__init__.py", line 1036, in connect
     self.disable_ssl_certificate_validation, self.ca_certs)
   File "/home/trex/workspace/myenv/lib/python2.7/site-packages/httplib2-0.9.2-py2.7.egg/httplib2/__init__.py", line 80, in _ssl_wrap_socket
     cert_reqs=cert_reqs, ca_certs=ca_certs)
   File "/usr/lib/python2.7/ssl.py", line 911, in wrap_socket
     ciphers=ciphers)
   File "/usr/lib/python2.7/ssl.py", line 520, in __init__
     self._context.load_verify_locations(ca_certs)
 IOError: [Errno 13] Permission denied

api_manager.py

  import oauth2 as oauth
  from settings import * #-- All global params
  class APIManager(object):
      """ """
     #init function goes here
    def get_api_content(self, consumer_key, consumer_secret, access_token, access_secret):
       """ Fetch API response using Oauth2 """
       try:
           consumer = oauth.Consumer(key=consumer_key, secret=consumer_secret) 
           token = oauth.Token(key=access_token, secret=access_secret) 
           client = oauth.Client(consumer, token) 
           uri = APIURL +  APIURI.format(account_id=SOME_ACC_ID)
           _, content = client.request(uri=uri, method="GET")
        except Exception as e:
            logerror("ERROR:", e)
            content = {}
       return content          

flaskapp.py

from flask import Flask, jsonify
from api_manager import APIManager
from settings import *
app = Flask(__name__)

@app.route("/")
def callapi():
    try:
        apiobj = APIManager()
        response = apiobj.get_api_content(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_SECRET)
    except Exception, e:
        logerror("ERROR:", e)
        return jsonify({})
    return jsonify(response)

Apache WSGI配置:

<VirtualHost _default_:443>
  ...
   # Default other config goes here...
  WSGIScriptAlias / /home/trex/workspace/scripts/appwsgi.py
  WSGIScriptReloading On
  <Directory "/home/trex/workspace/scripts">
    <Files "appwsgi.py">
       Require all granted
    </Files>
  </Directory>
</VirtualHost>

如果有任何其他问题我可以告诉您详情。

1 个答案:

答案 0 :(得分:1)

我的virtualenv的权限存在问题。以下文件有权限问题。

myenv/lib/python2.7/site-packages/httplib2-0.9.2-py2.7.egg/httplib2/cacerts.txt

这个简单的修复解决了我的问题:

chmod  -R 755  /home/trex/workspace/myenv