I'm trying to fix my script which sets up mail forwarding on Google Apps with the new OAuth2 authentication. I have the directory API working fine, but the forwarding stopped working last week with the username/password authentication.
I've updated my code based on another post here (Enable mail forwarding using a Google Apps Service Account), including what appears to be the same as what's labeled "Final Working Code" there, but cannot figure out what is wrong.
When authenticating, I receive the error below:
Traceback (most recent call last):
File "./tmp.py", line 26, in <module>
client.UpdateForwarding(username="[email protected]", enable=True, forward_to='[email protected]', action='ARCHIVE')
File "/usr/lib/python2.6/site-packages/gdata/apps/emailsettings/client.py", line 320, in update_forwarding
return self.update(new_forwarding, **kwargs)
File "/usr/lib/python2.6/site-packages/gdata/client.py", line 730, in update
desired_class=entry.__class__, **kwargs)
File "/usr/lib/python2.6/site-packages/gdata/client.py", line 307, in request
response, Unauthorized)
gdata.client.Unauthorized: Unauthorized - Server responded with: 401,
My code is as follows:
import gdata.gauth
import gdata.apps.emailsettings.client
from oauth2client.client import SignedJwtAssertionCredentials
service_account = My_Service_Account
adminEmail = My_Admin_Email
scope = 'https://apps-apis.google.com/a/feeds/emailsettings/2.0/'
keyfile = file(My_Key_Location,'rb')
key = keyfile.read()
keyfile.close()
credentials = SignedJwtAssertionCredentials(service_account, key, sub=My_Admin_Email, scope='https://apps-apis.google.com/a/feeds/emailsettings/2.0/')
auth = gdata.gauth.OAuth2Token(
credentials.client_id,#serviceEmail
credentials.client_secret,#private key
scope='https://apps-apis.google.com/a/feeds/emailsettings/2.0/',
access_token=credentials.access_token,
refresh_token=credentials.refresh_token,
user_agent=credentials.user_agent)
client = gdata.apps.emailsettings.client.EmailSettingsClient(domain='MyDomain')
auth.authorize(client)
client.UpdateForwarding(username="TestAccount", enable=True, forward_to=DestinationAddress, action='ARCHIVE')
I would have added comments to the previous post referenced above, but my account here is new (and it says I need a 50 reputation to comment). Any assistance on this would be greatly appreciated!