2016-04-19 3 views
0

個人のGoogleアカウントでGmail APIを試してみました。簡単にするため、consider the Python QuickStart example(これは問題ありません)。問題は、同じ方法でGmailにアクセスできないことです。続きApps for WorkにGmailにアクセスするには個人アカウントからGmail APIを使用しますか?

<HttpError 403 when requesting https://www.googleapis.com/gmail/v1/users/myworkemail%40myworkdomain.com/labels?alt=json returned "Delegation denied for [email protected]"> 

私は単にコードの私の仕事用メールアドレスを使用して私の個人的な電子メールアドレスを交換した場合...

results = service.users().labels().list(userId='myworkemail%40myworkdomain.com').execute() 

...私は、標準の代表団がエラーを拒否された取得します以前のStackExchangeの質問のヒントはほとんどありませんでした。私は、service account and an authorised API callを作成するための手順を試してみました。サービスアカウントのフォームは私の個人アカウントの開発者コンソールです...

credentials = ServiceAccountCredentials.from_json_keyfile_name('service_account.json', scopes=SCOPES) 
delegated_credentials = credentials.create_delegated('myworkemail%40myworkdomain.com') 
http_auth = delegated_credentials.authorize(httplib2.Http()) 
service = discovery.build('gmail', 'v1', http=http_auth) 

results = service.users().labels().list(userId='myworkemail%40myworkdomain.com').execute() 

...が、代わりに別のエラーが表示されます。注意すべき

File "qs.py", line 70, in main 
    results = service.users().labels().list(userId='myworkemail%40myworkdomain.com').execute() 
    File "/usr/local/lib/python2.7/site-packages/oauth2client/util.py", line 135, in positional_wrapper 
    return wrapped(*args, **kwargs) 
    File "/usr/local/lib/python2.7/site-packages/googleapiclient/http.py", line 755, in execute 
    method=str(self.method), body=self.body, headers=self.headers) 
    File "/usr/local/lib/python2.7/site-packages/googleapiclient/http.py", line 93, in _retry_request 
    resp, content = http.request(uri, method, *args, **kwargs) 
    File "/usr/local/lib/python2.7/site-packages/oauth2client/client.py", line 597, in new_request 
    self._refresh(request_orig) 
    File "/usr/local/lib/python2.7/site-packages/oauth2client/client.py", line 863, in _refresh 
    self._do_refresh_request(http_request) 
    File "/usr/local/lib/python2.7/site-packages/oauth2client/client.py", line 932, in _do_refresh_request 
    raise HttpAccessTokenRefreshError(error_msg, status=resp.status) 
oauth2client.client.HttpAccessTokenRefreshError: unauthorized_client: Unauthorized client or scope in request. 

もう一つのポイント:the API explorer examplesを私の仕事のアカウントとGmailのAPIリファレンスページの作業罰金上、マイアカウント」に認可として表示'私が望むのは同じ動作です:の場合 Gmailアカウント(個人または職場)は、明示的な承認の後、同じコードでアクセスできます。

答えて

2

Gmail API - Usage Limitsで指定されているように、プロジェクトのユーザーあたり250クォータ単位のユーザーレート制限を超過すると、HTTPステータスコードの応答が403になる可能性があります。

Manage Delegation Settingsには、クライアントアプリケーションが複数のデリゲートを作成するか、新しく作成されたデリゲートを取得するまでの間に遅延があることが推奨されます。また、Gmailアクセスを委任する各ユーザーには最大25人のデリゲートが許可されます。 Gmail委任に関するその他の注意事項については、ドキュメントを参照してください。

+0

助けてくれてありがとう、ありがとうございます。私はすぐに私の質問に答えを投稿します。 –

0

答え(非ウェブアプリケーション Python)は、一度見つけたら簡単です。

このPythonのクイックスタートアプリから更新get_credentials()コードされています

def get_credentials(): 
    home_dir = os.path.expanduser('~') 
    credential_dir = os.path.join(home_dir, '.credentials') 
    if not os.path.exists(credential_dir): 
     os.makedirs(credential_dir) 
    credential_path = os.path.join(credential_dir, 'gmail-python-quickstart.json') 

    store = oauth2client.file.Storage(credential_path) 
    credentials = store.get() 
    if not credentials or credentials.invalid: 
     flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES, login_hint = '[email protected]') 
     flow.user_agent = APPLICATION_NAME 
     if flags: 
      credentials = tools.run_flow(flow, store, flags) 
     else: # Needed only for compatibility with Python 2.6 
      credentials = tools.run(flow, store) 
     print('Storing credentials to ' + credential_path) 
    return credentials 

重要な変更がlogin_hint = 'a_new_accoun[email protected]'これは

flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES, login_hint = '[email protected]')への追加です私が見ることができる Python OAuth 2 API documentationのどこにも言及されていないが、典型的なGoogleのファッションでは here in the oauth2client documentationと書かれている。

関連する問題