1

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. When I replace the StringVar variable with regular strings, I don't have an issue. When I check the type of the variable from StringVar() and I convert it to a string, it returns types string. I've tried using .format() and I've tried just passing the get() function. 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">

USER is the admin account.

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

Eugene Gravel
  • 11
  • 1
  • 4
  • delegator.get() does in fact return a "regular string", Suggestion: in your print statement, don't just print the type of string but also print its value (i.e., print(type(string), string). If the printed value is 'user@domain.com" and its type is str, then it's got to work the same as if you hard-coded the string's value. Are you saying that it doesn't? – Paul Cornelius Dec 24 '19 at 02:20
  • Thank you for letting me know about the print command. It actually came out blank. This makes sense that the api call isn't working. Is there something about StringVar() that doesn't allow another variable to be assigned to it? – Eugene Gravel Dec 24 '19 at 17:59
  • A StringVar is a Python variable, so there is nothing that prevents you from binding another object to the same variable name. But I don't see where you are doing that. StringVar is a holder for a str. The str gets set by the StringVar.set method and accessed by the StringVar.get method. It also gets set automatically by tkinter when the delegator_entry text box changes. Are you typing "user@domain.com" (or whatever) into delegator_entry BEFORE clicking the "print" button? Otherwise the string will still have its initiali value, which is ''.. – Paul Cornelius Dec 24 '19 at 23:57

1 Answers1

0

I got it working. Paul Cornelius was right about me missing up the order. 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
Eugene Gravel
  • 11
  • 1
  • 4