我使用了本指南:Python 快速入门。
我还创建了一个 G-Suite 帐户来访问 Google Apps 域控制面板。
这是我插入帖子的代码:
__author__ = 'joannasmith@google.com (Joanna Smith)'
import httplib2
import pprint
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
SERVICE_ACCOUNT_EMAIL = 'servizio@projecttest-161515.iam.gserviceaccount.com'
SERVICE_ACCOUNT_PKCS12_FILE_PATH = './ProjectTest-9179fae1c392.json'
USER_EMAIL = 'its2016web@assicuraora.com'
SCOPES = ['https://www.googleapis.com/auth/plus.me','https://www.googleapis.com/auth/plus.stream.write']
def authenticate():
print 'Authenticate the domain for %s' % USER_EMAIL
credentials = ServiceAccountCredentials.from_json_keyfile_name(
SERVICE_ACCOUNT_PKCS12_FILE_PATH, scopes=SCOPES)
delegate_credentials = credentials.create_delegated(USER_EMAIL)
http = httplib2.Http()
http = delegate_credentials.authorize(http)
return build('plusDomains', 'v1', http=http)
def activitiesInsert(service):
user_id = 'me'
print('Insert activity')
result = service.activities().insert(
userId = user_id,
body = {
'object' : {
'originalContent' : 'Happy Monday! #caseofthemondays'
}
'access' : {
'items' : [{
'type' : 'domain'
}],
'domainRestricted': True
}
}).execute()
print('result = %s' % pprint.pformat(result))
if __name__ == '__main__':
service = authenticate()
activitiesInsert(service)
要检索 ClientID,我必须在服务帐户密钥上选中“在整个域 G Suite 上启用委派”,并且我还从 Google Apps 域控制面板向客户端 ID 添加了范围。
但是当我运行我的代码时,它会输出这个错误:
<HttpError 403 请求 https://www.googleapis.com/plusDomains/v1/people/me/activities?alt=json时 返回“禁止”>
为什么?
我需要以这种形式订阅吗?https://developers.google.com/+/web/api/rest/pages-signup
编辑:我为授权做了截图,对我来说似乎没问题。