Hi Prashant,

I had corrected this issue. The requestor_id must be present, and the value 
must be an email from the domain denoted by the consumer key, I thought that 
it had to be the user we were getting information. Attached is the new 
script. With it, everything worked fine for the labels.

But now, I am having some issues with XOAUTH on an imap connection. The 
script with this test is attached as emailsettings-imap.py. We are receiving 
errors for the accounts below only:

I had tried changing the xoauth_requestor_id for different values, but none 
worked for the users above. The others users, worked. I have noticed that 
this problem happens when using a consumer key from a different domain. For 
this foreign consumer key, I added the following scope only: *
https://mail.google.com/mail/b/* . But, for the original domain consumer 
key, with only this scope enabled, it gives the same error as the foreign 
one, only works when we set to enable access to all APIs. Does it have a 
missing scope?

Best regards,

Eduardo

-- 
You received this message because you are subscribed to the Google Groups 
"Google Apps Domain Information and Management APIs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-apps-mgmt-apis?hl=en.

#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Code to test de emailsettings, label retrieving.

Arguments: consumer_key consumer_secret domain user_1 user_2 ... user_n

consumer_key must be and email from the consumer_key domain, that will be
the requestor_id

If no user is done, will list the labels for all users.
'''

import sys
import gdata.apps.service
import gdata.apps.emailsettings.service

SIG_METHOD = gdata.auth.OAuthSignatureMethod.HMAC_SHA1


print "Start..."

# Getting parameters
requestor_id = sys.argv[1]
consumer_key = sys.argv[1][sys.argv[1].find('@')+1:]
consumer_secret = sys.argv[2]
domain = sys.argv[3]

if len(sys.argv) > 4:
  users = sys.argv[4:]
else:
  # Getting all users, if needed
  appsServiceClient = gdata.apps.service.AppsService(domain=domain)
  appsServiceClient.SetOAuthInputParameters(SIG_METHOD, consumer_key, consumer_secret=consumer_secret, two_legged_oauth=True)

  for entry in appsServiceClient.GetGeneratorForAllUsers():
        users = [user.login.user_name for user in entry.entry]

print "Processing these users: %s" % users
print

# Get all the labels
for user in users:
  print "-"*40
  emailSettingsService = gdata.apps.emailsettings.service.EmailSettingsService(domain=domain)
  emailSettingsService.SetOAuthInputParameters(SIG_METHOD, consumer_key, consumer_secret=consumer_secret, two_legged_oauth=True, requestor_id=requestor_id )

  try:
    labelsFeed = emailSettingsService.GetLabels(user)
  except Exception, e:
    print "User '%s' ERR" % user
    print e
    continue
  else:
    print "User '%s' OK" % user

print "-"*40
print "End"
#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Code to test de emailsettings, label retrieving.

Arguments: consumer_key consumer_secret domain user_1 user_2 ... user_n

consumer_key must be and email from the consumer_key domain, that will be
the requestor_id

If no user is done, will list the labels for all users.
'''

import sys
import gdata.apps.service
import gdata.apps.emailsettings.service
import oauth2 as oauth
import oauth2.clients.imap as imaplib
import random
import time


SIG_METHOD = gdata.auth.OAuthSignatureMethod.HMAC_SHA1

def executeFunction(function, *args, **keys):
  '''
  Execute a function, for at most 10 times, until it runs with no exception.
  '''
  for n in range(0,10):
    try:
      return function(*args, **keys)
    except Exception, e:
      waitTime = random.randint(0,1<<n)
      time.sleep(waitTime)
    else:
      break
  else:
      raise e

print "Start..."

# Getting parameters
requestor_id = sys.argv[1]
consumer_key = sys.argv[1][sys.argv[1].find('@')+1:]
consumer_secret = sys.argv[2]
domain = sys.argv[3]

if len(sys.argv) > 4:
  users = sys.argv[4:]
else:
  # Getting all users, if needed
  appsServiceClient = gdata.apps.service.AppsService(domain=domain)
  appsServiceClient.SetOAuthInputParameters(SIG_METHOD, consumer_key, consumer_secret=consumer_secret, two_legged_oauth=True)

  for entry in appsServiceClient.GetGeneratorForAllUsers():
        users = [user.login.user_name for user in entry.entry]

print "Processing these users: %s" % users
print

# Get all the labels
for user in users:
  print "-"*40
  emailSettingsService = gdata.apps.emailsettings.service.EmailSettingsService(domain=domain)
  emailSettingsService.SetOAuthInputParameters(SIG_METHOD, consumer_key, consumer_secret=consumer_secret, two_legged_oauth=True, requestor_id=requestor_id )

  
  try:
    labelsFeed = executeFunction(emailSettingsService.GetLabels, user)
  except Exception, e:
    print "Labels for '%s' ERR" % user
    print e
  else:
    print "Labels for '%s' OK" % user

  # Connect to the IMAP
  email = "%s@%s" % (user, domain)
  try:
    imap = executeFunction(imaplib.IMAP4_SSL, 'imap.gmail.com')
    url = "https://mail.google.com/mail/b/%(email)s/imap/?xoauth_requestor_id=%(escape_email)s" % {'email': email, 'escape_email': oauth.escape(email) }
    consumer = oauth.Consumer(consumer_key, consumer_secret)
    executeFunction(imap.authenticate, url, consumer, None)
  except Exception, e:
    print "IMAP for '%s' ERR" % user
    print e
  else:
    print "IMAP for '%s' OK" % user    

print "-"*40
print "End"


Reply via email to