using python how to add clickable image as Gmail Signature

Viewed 145

My requirement is using python we want to add image as gmail signature. once the image is clicked we want to redirect to video file.

I tried this manually and was successful to add signature with the following steps:

  1. Open google docs and uploaded an image
  2. Added hyperlink to the image
  3. Now copied the image into gmail signature section
  4. Once i click on the image it is sucessfully redirected to video file.

Using python, i want to achieve this in dynamic way

  1. I created the google drive credentials
  2. Enabled the google docs API
  3. Able to read the docs data
  4. Now, iam stuck how to add text as signature.

I am trying with the below approach

SCOPES = ['https://www.googleapis.com/auth/gmail.settings.basic','https://www.googleapis.c om/auth/gmail.labels','https://www.googleapis.com/auth/gmail.send']

creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', SCOPES)

GMAIL = discovery.build('gmail', 'v1', http=creds.authorize(Http()))
 addresses = GMAIL.users().settings().sendAs().list(userId='me',
fields='sendAs(isPrimary,sendAsEmail)').execute().get('sendAs')

for address in addresses:
  if address.get('isPrimary'):
    break
 rsp = GMAIL.users().settings().sendAs().patch(userId='me',
    sendAsEmail=address['sendAsEmail'], body=DATA).execute()
print("Signature changed to '%s'" % rsp['signature'])

The error am getting is

"code": 401, "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential

Pls suggest the way forward

Thanks vijay

1 Answers

Finally got it

SCOPES = 'https://www.googleapis.com/auth/gmail.settings.basic'
store = file.Storage('credentials.json')
creds =None
args.noauth_local_webserver = True

if not creds or creds.invalid:

  flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
  creds = tools.run_flow(flow, store,args)
GMAIL = discovery.build('gmail', 'v1', http=creds.authorize(Http()))

addresses = GMAIL.users().settings().sendAs().list(userId='me',
    fields='sendAs(isPrimary,sendAsEmail)').execute().get('sendAs')
for address in addresses:
  if address.get('isPrimary'):
    break
signature = {'signature':'<img src="http://myserver/mycard.JPG" alt="Trulli" 
  width="200" height="200">'}
 rsp = GMAIL.users().settings().sendAs().patch(userId='me',
    sendAsEmail=address['sendAsEmail'], body=signature).execute()
 print("Signature changed to '%s'" % rsp['signature'])
Related