[英]Python Tkinter StringVar() not being packaged in a JSON request
I'm having an issue with packaging a Tkinter StringVar() variable into a function that then takes the variable and passes it to Google's Gmail API.我在将 Tkinter StringVar() 变量打包到一个函数中时遇到问题,该函数然后获取该变量并将其传递给 Google 的 Gmail API。 When I replace the StringVar variable with regular strings, I don't have an issue.当我用常规字符串替换 StringVar 变量时,我没有问题。 When I check the type of the variable from StringVar() and I convert it to a string, it returns types string.当我从 StringVar() 检查变量的类型并将其转换为字符串时,它返回类型字符串。 I've tried using .format() and I've tried just passing the get() function.我试过使用 .format() 并且我试过只传递 get() 函数。 Neither is accepted.都不接受。
Using str(delegator.get()) or delegator.get() I receive this error code: https://www.googleapis.com/gmail/v1/users/me/settings/delegates?alt=json returned "Bad Request">使用 str(delegator.get()) 或 delegator.get() 我收到此错误代码:https://www.googleapis.com/gmail/v1/users/me/settings/delegates?alt=json 返回“错误请求">
USER is the admin account. USER 是管理员帐户。
Here's the code:这是代码:
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
I got it working.我让它工作了。 Paul Cornelius was right about me missing up the order.保罗·科尼利厄斯 (Paul Cornelius) 说我错过了订单是对的。 Here's the working code:这是工作代码:
class Delegation(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
delegator_entry = 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)
delegator_label = tk.Label(self, textvariable=delegator_entry)
delegator_label.grid(column=0, row=1)
delegator_entry = tk.Entry(self, width=30, textvariable=delegator_entry)
delegator_entry.grid(column=0, row=2)
pls_print = tk.Button(self,width=50, text="print",
command=lambda: self.let_me_set(credentials,delegator_entry))
pls_print.grid(column=0,row=3)
def let_me_set(self, credentials, delegator_entry):
global delg
delg = delegator_entry.get()
self.main(credentials,delg)
def main(self,credentials,delg):
my_creds = str(delg)
credentials_delegated = credentials.with_subject(my_creds) #this is the address that the delegation happens to.
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.