gpt4 book ai didi

python - 无法使用 facebook python sdk

转载 作者:行者123 更新时间:2023-11-28 21:51:48 26 4
gpt4 key购买 nike

我正在尝试使用来自 http://github.com/pythonforfacebook 的 SDK在一个新的 appengine 项目中,但是 dev-appserver 说找不到模块名称 facebook。

代码如下:

import facebook
import webapp2
import os
import jinja2
import urllib2

from google.appengine.ext import db
from webapp2_extras import sessions


FACEBOOK_APP_ID = "xxxxx"
FACEBOOK_APP_SECRET = "xxxxx"

config = {}
config['webapp2_extras.sessions'] = dict(secret_key='')

class User(db.Model):
id = db.StringProperty(required=True)
created = db.DateTimeProperty(auto_now_add=True)
updated = db.DateTimeProperty(auto_now=True)
name = db.StringProperty(required=True)
profile_url = db.StringProperty(required=True)
access_token = db.StringProperty(required=True)


class BaseHandler(webapp2.RequestHandler):
"""Provides access to the active Facebook user in self.current_user
The property is lazy-loaded on first access, using the cookie saved
by the Facebook JavaScript SDK to determine the user ID of the active
user. See http://developers.facebook.com/docs/authentication/ for
more information.
"""
@property
def current_user(self):
if self.session.get("user"):
# User is logged in
return self.session.get("user")
else:
# Either used just logged in or just saw the first page
# We'll see here
cookie = facebook.get_user_from_cookie(self.request.cookies,
FACEBOOK_APP_ID,
FACEBOOK_APP_SECRET)
if cookie:
# Okay so user logged in.
# Now, check to see if existing user
user = User.get_by_key_name(cookie["uid"])
if not user:
# Not an existing user so get user info
graph = facebook.GraphAPI(cookie["access_token"])
profile = graph.get_object("me")
user = User(
key_name=str(profile["id"]),
id=str(profile["id"]),
name=profile["name"],
profile_url=profile["link"],
access_token=cookie["access_token"]
)
user.put()
elif user.access_token != cookie["access_token"]:
user.access_token = cookie["access_token"]
user.put()
# User is now logged in
self.session["user"] = dict(
name=user.name,
profile_url=user.profile_url,
id=user.id,
access_token=user.access_token
)
return self.session.get("user")
return None

def dispatch(self):
"""
This snippet of code is taken from the webapp2 framework documentation.
See more at
http://webapp-improved.appspot.com/api/webapp2_extras/sessions.html
"""
self.session_store = sessions.get_store(request=self.request)
try:
webapp2.RequestHandler.dispatch(self)
finally:
self.session_store.save_sessions(self.response)

@webapp2.cached_property
def session(self):
"""
This snippet of code is taken from the webapp2 framework documentation.
See more at
http://webapp-improved.appspot.com/api/webapp2_extras/sessions.html
"""
return self.session_store.get_session()


class HomeHandler(BaseHandler):
def get(self):
template = jinja_environment.get_template('example.html')
self.response.out.write(template.render(dict(
facebook_app_id=FACEBOOK_APP_ID,
current_user=self.current_user
)))

def post(self):
url = self.request.get('url')
file = urllib2.urlopen(url)
graph = facebook.GraphAPI(self.current_user['access_token'])
response = graph.put_photo(file, "Test Image")
photo_url = ("http://www.facebook.com/"
"photo.php?fbid={0}".format(response['id']))
self.redirect(str(photo_url))


class LogoutHandler(BaseHandler):
def get(self):
if self.current_user is not None:
self.session['user'] = None

self.redirect('/')

jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__))
)

app = webapp2.WSGIApplication(
[('/', HomeHandler), ('/logout', LogoutHandler)],
debug=True,
config=config
)

这里是错误:

INFO 2015-03-29 22:18:30,182 module.py:737] default: "GET / HTTP/1.1" 500 - ERROR 2015-03-29 22:19:07,986 wsgi.py:263] Traceback (most recent call last): File "/opt/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle handler = config_handle.add_wsgi_middleware(self.LoadHandler()) File "/opt/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler handler, path, err = LoadObject(self._handler) File "/opt/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject obj = __import(path[0]) File "/home/juliano/Documents/Projetos/Fb2/fb.py", line 1, in import facebook ImportError: No module named facebook INFO 2015-03-29 22:19:08,003 module.py:737] default: "GET / HTTP/1.1" 500 -

我正在使用这些命令来添加 facebook python sdk:

virtualenv facebookenv
source facebookenv/bin/activate
pip install facebook-sdk

最佳答案

您没有告诉我们您是如何导入库的,所以我假设您正在像其他库一样对待它,进行某种全局安装。

在 GAE 中,您必须将所需的所有库都放在与您的项目相同的目录中,以便它们与您的代码一起上传并可以访问。请记住,您在沙箱中运行,因此必须随身携带所需的一切。

这意味着您需要下载 sdk 的 facebook 目录,并将其放在您拥有 app.yamlimport 的同一目录中 就会成功。

关于python - 无法使用 facebook python sdk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29336499/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com