我遇到了一个问题,就是如何将一个Tkinter StringVar()变量打包成一个函数,然后将该变量传递给Google的gmailapi。当我用常规字符串替换StringVar变量时,我没有问题。当我从StringVar()检查变量的类型并将其转换为字符串时,它将返回string类型。我试过使用.format(),也试过只传递get()函数。两者都不接受
使用str(delegator.get())或delegator.get(),我收到以下错误代码:https://www.googleapis.com/gmail/v1/users/me/settings/delegates?alt=json 返回“错误请求”>
用户是管理员帐户
代码如下:
class Delegation(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.delegator = tk.StringVar()
key_path = 'service_id.json'
API_scopes =['https://www.googleapis.com/auth/gmail.settings.basic',
'https://www.googleapis.com/auth/gmail.settings.sharing']
credentials = service_account.Credentials.from_service_account_file(key_path,scopes=API_scopes)
button1 = tk.Button(self,width=50, text="BACK",
command=lambda: controller.show_frame(StartPage))
button1.grid(column=0,row=0)
pls_print = tk.Button(self,width=50, text="print",
command=lambda: self.main(credentials,self.delegator))
pls_print.grid(column=0,row=3)
delegator_entry = tk.Entry(self, width=30, textvariable=self.delegator)
delegator_entry.grid(column=0, row=2)
delegator_label = tk.Label(self, text="Please enter email address \nyou want to access.")
delegator_label.grid(column=0, row=1)
def main(self,credentials,delegator):
string = 'user@domain.com'#or str(self.delegator.get()) #both return user@domain.com
print(type(string)) # returns <class 'str'>
credentials_delegated = credentials.with_subject('{}'.format(string))
gmail_service = build("gmail","v1",credentials=credentials_delegated)
gmail_service.users().settings().delegates().create(userId='me', body={'delegateEmail': USER, "verificationStatus": "accepted"}).execute()
assert gmail_service
我成功了。保罗·科尼利厄斯说得对,我错过了订单。以下是工作代码:
相关问题 更多 >
编程相关推荐