문제

데코레이터가 있습니다 :

http = httplib2.Http()
service = discovery.build('gmail', 'v1', http=http)
# Creating decorator for OAuth2 account.
decorator = appengine.oauth2decorator_from_clientsecrets(
    CLIENT_SECRETS,
    scope='https://www.googleapis.com/auth/gmail.readonly',
    message=MISSING_CLIENT_SECRETS_MESSAGE)
.

및 클래스를 사용하는 클래스 :

class CSVGeneratorHandler(webapp2.RedirectHandler):
    @decorator.oauth_required
    def get(self):
        http = decorator.http()
        first_messages = service.users().messages().list(userId='me').execute(http=http)
        template = JINJA_ENVIRONMENT.get_template('templates/success.html')
        self.response.write(template.render({}))
.

멋지게 작동합니다.

그러나 API 요청 service.users ()를 이동하려고 할 때 message (). list (userid= 'me'). (http= http) (지연됨)derfer (get_mails)) 오류가 발생했습니다 : "로그인 필요".

i get_mails 함수는 주 프로그램에서 컨텍스트 또는 / 및 자격 증명을 수신해야한다는 것을 이해합니다.그러나 나는 그것을하는 방법을 이해하지 못합니다.

누군가가 나를 도울 수 있습니까?

도움이 되었습니까?

해결책

몇 시간 후에 답변을 찾았습니다.

먼저 자격 증명을 일부 장소에 저장해야합니다.우리는 당신이 당신에게 특별한 필드 유형을 가지고 있기 때문에 :

from oauth2client.appengine import StorageByKeyName  
from google.appengine.ext import db

class CredentialsModel(db.Model):
  credentials = CredentialsProperty()
.

다음 단계는 Decorator없이 인증을 설정하고 해당 작업을 대기열에 보내기 후에 만 다음 단계를 수행하는 것입니다.

    user = users.get_current_user()
    flow = flow_from_clientsecrets(os.path.join(os.path.dirname(os.path.realpath(__file__)), CLIENT_SECRETS),
                           scope='https://www.googleapis.com/auth/gmail.readonly',
                           redirect_uri='http://127.0.0.1:8080/success')
    auth_uri = str(flow.step1_get_authorize_url())
    code = self.request.get('code')
    if not code:
        return self.redirect(auth_uri)
    credentials = flow.step2_exchange(code)

    storage = StorageByKeyName(CredentialsModel, user.user_id(), 'credentials')
    storage.put(credentials)

    deferred.defer(get_mails, user.user_id())
.

지연된 기능에서 자격 증명을 얻는 것을 잊지 마십시오!

def get_mails(user_id):
    storage = StorageByKeyName(CredentialsModel, user_id, 'credentials')
    credentials = storage.get()

    http = httplib2.Http()
    http = credentials.authorize(http)

    all_messages = []
    service = discovery.build('gmail', 'v1', http=http)
    first_messages = service.users().messages().list(userId='me').execute()
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top