2013-10-24 61 views
0

我試圖將雲端點合併到我的應用程序中,目前我正在使用Python Quickstart來進行概念驗證。當我嘗試調用一種方法將卡片發送到我的眼鏡時,我遇到了問題。以下是我的代碼,請忽略缺少的縮進。雲端點 - Google Glass對象沒有屬性'mirror_service'

@endpoints.api(name='tasks', version='v1', 
description='API for TaskAlert Management', 
allowed_client_ids=[CLIENT_ID, endpoints.API_EXPLORER_CLIENT_ID]) 
class TaskAlertApi(remote.Service): 
@endpoints.method(Task, Task, 
name='task.insert', 
path='tasker', 
http_method='POST') 
def insert_task(self, request): 

TaskModel(author=request.author, content=request.content, date=request.date).put() 
themirror = MainHandler() 

themirror._insert_map_with_distance_from_home() 

return request 

所以,當 「themirror._insert_map_with_distance_from_home()」 被稱爲我收到以下錯誤。有沒有人有什麼建議?我正嘗試從myappspot.com/_ah/api/explorer調用此方法。

in _insert_map_with_distance_from_home 
    self.mirror_service.timeline().insert(body=body).execute() 
AttributeError: 'MainHandler' object has no attribute 'mirror_service' 

回答

2

恐怕你不得不重新考慮一下你的代碼,但是我會盡力得到這裏解釋的基礎知識。

主要問題是,當實際接收HTTP請求時,MainHandler會執行相當多的操作。最重要的是主要處理者get方法的@util.auth_required修飾器中發生的情況,該方法實際上創建了mirror_service,併爲當前用戶進行了身份驗證。當您直接從您的代碼訪問MainHandler時,實際上並沒有發生這種情況,因此沒有可用的mirror_service(這會導致出現錯誤)。

由於調用端點的方式與調用普通RequestHandlers的方式完全不同,因此您也不能依賴存儲的會話憑證或類似方法來將端點用戶與鏡像用戶進行匹配。

基本上你需要做的是在你的端點方法中創建一個新的mirror_service

爲此,您必須調用您的API進行身份驗證(將鏡像API範圍添加到身份驗證範圍)。然後,您可以從請求標頭中提取使用的access_token,並使用此access_token創建OAuth2Credentials以創建mirror_service。

不完整的,因爲它很難說,但也帶來一些代碼片段不知道你的實際代碼:

import os 
from oauth2client.client import AccessTokenCredentials 

# extract the token from request 
if "HTTP_AUTHORIZATION" in os.environ: 
    (tokentype, token) = os.environ["HTTP_AUTHORIZATION"].split(" ") 

# create simple OAuth2Credentials using the token 
credentials = AccessTokenCredentials(token, 'my-user-agent/1.0') 

# create mirror_service (using the method from util.py from the quickstart(
mirror_service = create_service('mirror', 'v1', credentials) 

當然,那麼你也將不得不改變_insert_map_with_distance_from_home使用這個mirror_service對象,但移動這種方法無論如何,遠離你的MainHandler在這方面會更有意義。

+0

謝謝你的指導。我正在通過它,也從http://stackoverflow.com/questions/18213622/using-additional-google-apis-in-my-glassware-sharing-to-g-accounts整合評論這裏是錯誤我'目前正在獲取。遇到來自ProtoRPC方法實現的意外錯誤:HttpError(當請求https://www.googleapis.com/mirror/v1/timeline?alt=json返回「Insufficient Permission」時,<)任何想法? – marty331

+1

當您對API的請求進行身份驗證(我假設您仍在使用API​​ Explorer進行測試?)時,您是否已將「https:// www.googleapis.com/auth/glass.timeline」添加爲其他範圍?因爲否則,您無權使用獲取的訪問令牌寫入時間表。 – Scarygami

+0

Scarygami,那正是我錯過的!我現在已經連接了端點,並且能夠將時間線卡發送給Glass!我無法感謝你的幫助! – marty331