我正在尝试模拟超级管理员帐户以使用共享联系人 API 在目录中创建新联系人,在使用 gdata 客户端库后,即使我使用与授权另一个 API 相同的方法,我也无法授权我的请求调用目录 API 而不是域共享联系人。
这是我的代码:
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
import gdata.contacts
import gdata.contacts.data
import gdata.contacts.client
import atom
class TestView(APIView):
SERVICE_ACCOUNT_EMAIL = (
"x-serviceaccount@x.iam.gserviceaccount.com"
)
SERVICE_ACCOUNT_PKCS12_FILE_PATH = "C:\\Users\\Hoda\\Desktop\\xtools\\x\\Backend\\x-x.p12"
def get(self, request, *args, **kwargs):
credentials = ServiceAccountCredentials.from_p12_keyfile(
self.SERVICE_ACCOUNT_EMAIL,
self.SERVICE_ACCOUNT_PKCS12_FILE_PATH,
"notasecret",
scopes=[
'https://www.googleapis.com/auth/admin.directory.user',
'https://www.googleapis.com/auth/admin.directory.user.security',
'https://www.googleapis.com/auth/admin.directory.group',
'https://www.google.com/m8/feeds/contacts/'
],
)
gd_client = gdata.contacts.client.ContactsClient()
credentials = credentials.create_delegated('admin@admin.com')
service = build('admin', 'something', credentials=credentials)
#while using the directory API I wrote directory_v1 instead of
#something and now I have no
#idea what to write and is that even the right way to authorize
#the service account here or not
new_contact = gdata.contacts.data.ContactEntry()
new_contact.name = gdata.data.Name(
given_name=gdata.data.GivenName(text="Mohamed"),
family_name=gdata.data.FamilyName(text="Safi"),
full_name=gdata.data.FullName(text="Mohamed Safi"),
)
new_contact.content = atom.data.Content(text="Notes")
new_contact.email.append(
gdata.data.Email(
address="liz@gmail.com",
primary="true",
rel=gdata.data.WORK_REL,
display_name="E. Bennet",
)
)
new_contact.email.append(
gdata.data.Email(address="liz@example.com", rel=gdata.data.HOME_REL)
)
new_contact.phone_number.append(
gdata.data.PhoneNumber(
text="(206)555-1212", rel=gdata.data.WORK_REL, primary="true"
)
)
new_contact.phone_number.append(
gdata.data.PhoneNumber(text="(206)555-1213", rel=gdata.data.HOME_REL)
)
new_contact.im.append(
gdata.data.Im(
text="liz@gmail.com",
primary="true",
rel=gdata.data.HOME_REL,
protocol=gdata.data.GOOGLE_TALK_PROTOCOL,
)
)
new_contact.structured_postal_address.append(
gdata.data.PostalAddress(
rel=gdata.data.WORK_REL,
primary="true",
street=gdata.data.Street(text="1600 Amphitheatre Pkwy"),
city=gdata.data.City(text="Mountain View"),
region=gdata.data.Region(text="CA"),
postcode=gdata.data.Postcode(text="94043"),
country=gdata.data.Country(text="United States"),)
)
contact_entry = gd_client.create_contact(new_contact)
print("Contact's ID: %s" % contact_entry.id.text)
return Response(contact_entry)
所以,现在我只想知道如何授权该服务帐户将数据写入我的目录
旁注:我已在我的管理控制台中为服务帐户提供了所有所需的授权