2

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

inzack
  • 53
  • 6
  • I feel you should go back a bit. What library you are using to automate/send email? From your description the singnature is nothing but `` tag with `href` in html. definitely easier to set the Signature from html source – Equinox Sep 14 '20 at 11:15
  • Thats a good idea of adding thru html tags. currently am not using any library. can u suggest some library using javascript or python to add image as gmail signature – inzack Sep 15 '20 at 06:23
  • Is it part of Google Apps where you want to change all users signature or just looking for your own ? – Equinox Sep 15 '20 at 08:30
  • Its not part of Google apps, we are trying to handle this using python – inzack Sep 15 '20 at 08:49
  • I updated the code, could u pls through some light how to take it forward – inzack Sep 17 '20 at 11:18
  • I will try when i have some time meanwhile you can look at [this](https://gist.github.com/timrichardson/a43462aedc0797ecb76c48deb9c96d36) and [this](https://github.com/googleapis/google-api-python-client/issues/644) and [this](https://stackoverflow.com/questions/38534801/google-spreadsheet-api-request-had-insufficient-authentication-scopes). – Equinox Sep 17 '20 at 11:31

1 Answers1

2

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'])
Asha Datla
  • 126
  • 1
  • 11