我在将Tkinter StringVar()变量包装到一个函数中时遇到问题,该函数随后将该变量传递给Google的Gmail API。当我用常规字符串替换StringVar变量时,没有任何问题。当我从StringVar()检查变量的类型并将其转换为字符串时,它将返回字符串类型。我尝试使用.format(),并且尝试仅传递get()函数。都不被接受。

使用str(delegator.get())或delegator.get()我收到此错误代码:https://www.googleapis.com/gmail/v1/users/me/settings/delegates?alt=json返回了“错误请求“>

USER是管理员帐户。

这是代码:

   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 = '[email protected]'#or str(self.delegator.get()) #both return [email protected]
            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

最佳答案

我知道了保罗·科尼利厄斯(Paul Cornelius)对我错失命令表示正确。这是工作代码:

    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

关于python - Python Tkinter StringVar()未打包在JSON请求中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59462762/

10-10 23:06