How do I log into Google through python requests? - python

I'm making an API using Python requests, and HTTP GET is working fine, but I'm having a little bit of trouble with HTTP POST. So, as with a lot of websites, you can read information, but in order to make a post request (such as following a user, or writing a post), you need to have an authenticated session. THIS website, uses google to log in. Normally, I would just pass the username:password into the POST request formdata to log in, but this google thing is pretty wonky (and quite frankly I'm not that experienced). Does anyone have a reference or an example to help me out? ;/

I do not know about python requests but to send an email its as easy as this
import yagmail
yagmail.SMTP(emailh).send(email, subject, body)
#emailh = your email (just username no #gmail.com)
#email = send to (full email including domain ***#gmail.com or ***#outlook.com)
#subject = subject of the message
#body = body of the message
Even better
emailh = raw_input('Your email: ')
email = raw_input('Send to: ')
subject = raw_input('Subject: ')
body = raw_input('Body: ')
yagmail.SMTP(emailh).send(email, subject, body)
print('Email Sent.')
If this is what you are talking about anyway.
This page might be useful link

Related

Alexa fetching user email using python

I am trying to make an Alexa skill using python backend.
I am using amazon developer console to create model and code backend.
I want to retrieve user email address.
I would appreciate if you could provide me with sample code. I tried many methods but none were working.
here are some codes I tried :
https://github.com/alexa/alexa-skills-kit-sdk-for-python/tree/master/samples/GetDeviceAddress
I know this is device address but this was also not working, and I thought if i could get address I can get email.
Everything mentioned online is for Node, and I want to make my backend on python
As specified in the official documentation you need to make an API call to the ASK.
For email you need to call your api_endpoint followed by /v2/accounts/~current/settings/Profile.email
For me endpoint is : https://api.eu.amazonalexa.com therefore the complete url becomes :
https://api.eu.amazonalexa.com/v2/accounts/~current/settings/Profile.email
As far as adding the token to authorize the request it can be done by using the requests library by passing header to the get request. You can learn more about that from here
The final code should then look something like this :
#Fetching access token
accesstoken = str(handler_input.request_envelope.context.system.api_access_token)
#Fetching user emailaddress from ASK API
endpoint = "https://api.eu.amazonalexa.com/v2/accounts/~current/settings/Profile.email"
api_access_token = "Bearer " + accesstoken
headers = {"Authorization": api_access_token}
r = requests.get(endpoint, headers=headers)
email = r.json()
Hope this helps, Cheers!

How do I send multipart HTML and PLAIN Formatted emails, through the GMAIL-API for python

I have a question related to last answer in How do I send HTML Formatted emails, through the gmail-api for python but unfortunately the answer does not work for me. If I attach both the 'plain' and 'html' parts, it only accepts the LAST 'attach' call I make. That is, if I attach as 'plain' AFTER 'html', it only sends as 'plain',(which looks unappealing on devices/apps with HTML rendering)., but if I attach the 'html' AFTER 'plain', it only sends the 'html' format (which looks bad on devices/apps without HTML rendering). Unlike the person who posted that question, I do need both parts because some of the devices/apps that receive my emails do not render HTML and need the plain text part.
This is not a problem if I used 'smtplib' instead of GMAIL-API, but I want to use gmail api for better security in my app.
Here is my code:
message = MIMEMultipart('alternative')
message['to'] = to_email
message['from'] = from_email
message['subject'] = subject
body_plain = MIMEText(email_body,'plain')
message.attach(body_plain)
body_html_format="<u><b>html:<br>"+email_body+"</b></u>"
body_html = MIMEText(body_html_format,'html')
message.attach(body_html) # PROBLEM: Will only send as HTML since this was added LAST.
raw_string = base64.urlsafe_b64encode(message.as_bytes()).decode()
request = service.users().messages().send(userId='my.email#gmail.com',body={'raw':raw_string})
message = request.execute()
Thanks and regards,
Doug

How to send hyperlink with SendGrid using Python

I'm trying to send a simple mail with SendGrid which must contain one hyperlink.
I'm not doing anything fancy, just following the documentation example with some changes
import os
from sendgrid.helpers.mail import *
sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("test#example.com")
to_email = To("test#example.com")
subject = "Sending with SendGrid is Fun"
content = Content("text/html", '<html>google</html>')
mail = Mail(from_email, to_email, subject, content)
response = sg.client.mail.send.post(request_body=mail.get())
It looks fine to me, but once I run the script and the mail is sent, it shows up like plain text I cannot click on.
I also tried many other combinations removing the <html> tag, using single and double quotes with the backslash, but nothing really worked. I even tried to do the same thing without the Mail Helper Class, but it didn't work.
Thanks very much for the help.
content = Content(
"text/html", "Hi User, \n This is a test email.\n This is to also check if hyperlinks work <a href='https://www.google./com'> Google </a> Regards Karthik")
This helped me. I believe you don't need to mention the html tags

oauth2 without api key

I'm dead lost :)
Goal is to logon to a web site that uses OAuth2. However the section I need to run, doesn't have an API associated with it. So I need to login, just using the username and password, and then navigate to the page in question and do a screen scrape to get my data.
I'm sure the problem isn't at the web site it's sitting at this keyboard. But i've searched for examples and tried a whole bunch of guesses, but nothing is working
Help would be gratefully accepted.
import sys
import requests
import oauth2 as oauth
r = requests.get(logon_url)
consumer = oauth.Consumer(key=user, secret=password)
client = oauth.Client(consumer)
resp, content = client.request(r.url, "GET")
token_url = resp['content-location']
# At this point i'm lost i'm just guessing on the rest
# the next doesn't give an error but i'm sure it's wrong
resp2, content2 = client.request(token_url, 'GET')
# save the cookie, i do have a cookie but not sure what i have
auth_token = resp['set-cookie']
Like so many things, it's just a user error
code to get me to the page is so simple. And the following code does the trick. Thanks to Furas for the pointer.
with requests.session() as s1:
# get login form
r = s1.get(logon_url)
# post the username and password
resp = s1.post(r.url,data=payload)
# get the admin page
resp2 = s1.get(page_url)

sending emails in python weird behaviour

I am working on a piece of code that regularly sends emails from 4 different accounts, 2 of them are gmail accounts and the other 2 are yahoo accounts. When I started writing the code I was able to send all the emails from both the gmail accounts using the following piece of code:
def sendGmail(self, fromaddr, toaddr, username, password,
email_body, email_subject
):
# Build the email
msg = MIMEText(email_body)
msg['Subject'] = email_subject
msg['From'] = fromaddr
msg['To'] = toaddr
try:
# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddr, msg.as_string())
server.quit()
print "email sent: %s" % fromaddr
except Exception as e:
print "Something went wrong when sending the email %s" % fromaddr
print e
As I said, this piece of code worked perfectly, now that I add the sendYahoomail(), which is a different method, I always get (530, 'Access denied') as an exception for using sendGmail(). I'm pretty sure it's got nothing to do with my Yahoo method, and I can login from the browser with the gmail credentials correctly.
What can possibly be wrong, or just Gmail doesn't want me to send through code ?
I tried your code above with my Gmail account (actually it's a Google Apps account, but that should be identical) and it works fine for me. I even tried an incorrect "From" header and also using the incorrect source address at the SMTP level - both times Gmail allowed me to send the email, but seemed to quietly fix up the header on egress. Still, you might like to double-check your from address matches your username. Also try using the full email address as the username if you're currently just using the part before the #.
I also tried an incorrect password and received error 535, which isn't the same as what you're seeing. Since I have 2-factor authentication enabled I also tried my real password instead of an application-specific one and that still gave a 535 error (but with the message "application-specific password required").
Is it possible that your ISP has set up something that's intercepting SMTP connections to Gmail? Seems unlikely, although my ISP once blocked access to Gmail on port 587 although port 465 still worked. Perhaps you could try using smtplib.SMTP_SSL on port 465 just in case and see if that gives you any more joy.
You could also try sending to addresses at different providers in case Gmail is rejecting the send for that reason (e.g. if the other provider has been got on to a spam blacklist, for example). Also, if it's possible your email looks like spam, try you code with a message subject and body that's close to an authentic email and see if that helps.
You might find this helpful: https://support.google.com/mail/answer/14257
Basically, Google has detected an attempt to sign in from a server that it considers unfamiliar, and has blocked it. The link above lets you attempt to unblock it.
Sometimes it has worked for me, and sometimes not.

Categories

Resources