I'm currently trying to write a script and I would need to access my GMail contacts to finish it. I tried googling that a bit and it looks like there are a few libraries to do that, but some answers seems very old, and some others aren't that clear.
What is the correct way currently to access the contacts API from a script ? Using https://github.com/google/gdata-python-client I assume ?
Any recent response seems to involve the user copying a link into his browser by hand, getting redirected to some non-existent URL and being asked to copy that URL back in the script to parse the code in it. That seems perfectly ridiculous, I do hope there is a proper way to access the API for non-web applications ?
I'll be the only user of that script so I don't mind having to put my full e-mail address and password in if that makes it easier, as long as I don't have to keep re-authenticating by hand. Just want to authorize it once and have it work forever. I'm just trying to find the contact's name from the phone number so I don't even need to access everything.
Here is what I'm using in the end :
if arg == "--init":
gflow = OAuth2WebServerFlow(client_id='<ID>', client_secret='<secret>',
scope='https://www.googleapis.com/auth/contacts.readonly',
redirect_uri='http://localhost');
gflow.params['access_type'] = 'offline';
auth_uri = gflow.step1_get_authorize_url();
weechat.prnt("", "Please go to " + auth_uri + " and come back here with the code. Run /gauth code");
else:
credentials = gflow.step2_exchange(arg);
storage.put(credentials);
http = httplib2.Http();
http = credentials.authorize(http);
people = build('people', 'v1', http=http);
Works fine with that. I do need to copy / paste the URL by hand and then get the code out of the redirection, but since I put the access_type to offline I have to do that only once.
Related
I am not sure if i can ask about the Spotify API but saw no other subreddit to put in.
I am trying to connect to Spotify API without the use of external packages (like Spotipy).
I am reading their documentation here:
I decided to use the PKCE since I want to distribute a software and the user only needs to give their client_id (its open source)
In that case, I need to create a "code challenge", this is what I have:
SpotifyCodeChallenge = base64.b64encode(hashlib.sha256(SpotifyCodeVerifier.encode()))
Yet this is the error I get:
How would I fix this?
SpotifyCodeChallenge = base64.b64encode(hashlib.sha256(SpotifyCodeVerifier.encode('utf-8')).digest())
This will prob do it.
I'm currently attempting to use spotipy, a python3 module, to access and edit my personal Spotify premium account. I've followed the tutorial on https://github.com/plamere/spotipy/blob/master/docs/index.rst using the util.prompt_for_user_token method by entering the necessary parameters directly (username, client ID, secret ID, scope and redirect uri). Everything seems to be fine up to this part. My code (fillers for username, client id and client secret for security reasons) :
code
It opens up my default web browser and redirects me to my redirect url with the code in it. At this point, I copy and paste the redirect url (as prompted) and hit enter. It returns the following error:
Error
My redirect uri is 'http://google.com/' for this specific example. However, I've tried multiple redirect uris but they all seem to produce the same error for me. (and yes, I did set my redirect uri as whitespace for my application). I've spent hours trying to fix this issue by looking at online tutorials, trying different redirect urls, changing my code but have yet to make any progress. I'm hoping I am just overlooking a simple mistake here! Any feedback on how to fix this is much appreciated!
If it matters: I'm using the IDE PyCharm.
I had to use two different solutions to deal with the redirect_uri issue depending on which IDE I was using. For Jupyter Lab/Notebook, I could use a localhost for the redirect_url
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id="your_client_id", client_secret="your_client_secret", redirect_uri="https://localhost:8890/callback/", scope="user-library-read"))
For Google Colab, I had to use a publicly accessible website. I think "https://google.com/" should work but I used my band's website so I'd remember that the redirect_uri had to match the one in your Spotify Develop dashboard settings.
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id="your_client_id", client_secret="your_client_secret", redirect_uri="https://yourwebsite.com/", scope="user-library-read"))
I just ended up using my bands website because it was easier for me to remember. Make sure to go to the Spotify developer dashboard (https://developer.spotify.com/dashboard/applications) and match the redirect_uri with what you are planning to use at that time.
I think it is your redirect URL - working for me with:
import os
import spotipy.util as util
# credentials
user = 'username'
desired_scope = 'playlist-modify-private'
id = os.environ.get('SPOT_CLIENT')
secret = os.environ.get('SPOT_SECRET')
uri = 'https://localhost'
token = util.prompt_for_user_token(username=user,
scope=desired_scope,
client_id=id,
client_secret=secret,
redirect_uri=uri)
I think for your redirect url spotify requires the initial http(s) part - don't forget to add it to the white-list in your Spotify for Developers app too, as otherwise you will get 'invalid-redirect-uri'.
I'm currently working on a python-based app engine website and am looking to add google identity toolkit functionality but am getting stuck on the implementation of password resets and changes to email address..
Have been able to get the python quickstart example (https://developers.google.com/identity/toolkit/web/quickstart/python) working properly but even using this, I haven't been able to properly set up the password reset and email change components
I've been going through the google groups for this at https://groups.google.com/forum/#!forum/google-identity-toolkit but can't seem to be able to find detailed steps or sample code based off python
Would anybody have any ideas or can point me in the right direction? Much appreciated!
After some trial and error, I've been able to get this to work by:
creating a gitkit instance using the gitkit server config json
calling the GetOobResult function, which sends back a dict containing the reset link, among other information (This is the crux)
finally, to get the user notification to work properly you'll need to return a json dump with {'success' : true}
Here are the key lines of code I used -- should note that this doesn't include the email sending portion with the password reset link, which you have to implement separate from gitkit..
server_config_json = os.path.join(os.path.dirname(__file__), 'gitkit-server-config.json')
gitkit_instance = gitkitclient.GitkitClient.FromConfigFile(server_config_json)
output = gitkit_instance.GetOobResult(self.request.POST,self.request.remote_addr)
if output:
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(json.dumps({'success': True} ))
I've playing around in Python and the Facebook API - I'm fairly new to it! I'm copying and pasting an access token into my Python app from the Graph API Explorer as oauth_access_token manually just before execution. The simple app is a loop which performs some actions based on the status of some inputs. The loop also refreshes the token so that once the app is started with a new token, it should keep going for a longer amount of time. Snippet posted below, with IDs anonymized:
oauth_access_token="ABCDEFGHIJL"
auth_str="/oauth/access_token?grant_type=fb_exchange_token&client_id=XYZ1ABC&client_secret=[ABC2XYZ&fb_exchange_token="+oauth_access_token
graph = facebook.GraphAPI(oauth_access_token)
oauth_access_token = str(graph.request(auth_str)['access_token'])
print oauth_access_token
profile = graph.request("/me/notifications")
profile2 = graph.request("/me/inbox?fields=unseen")
I realize this isn't particularly elegant way of doing it. Bizarrely, when I do this on Windows, it works fine and I have access to the my notifcations and messages. When I do this on my Mac or Ubunutu it doesn't work and I get this error...I think it has something to do with Encoding, but I'm not sure what nor how to fix it?
Malformed access token ABCDEFGHIJK?access_token=ABCDEFGHIJK
I am trying to save a file (audio/mp3 in this case) to the App Engine blobstore, but with mixed success. Everything seems to work, a file is saved in the blobstore, of the right type, but it essentially empty (1.5kB vs. the expected 6.5kB) and so won't play. The URL in question is http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=revenues+in+new+york+were+56+million
The app engine logs do not show anything unusual - all parts are executing as expected... Any pointers would be appreciated!
class Dictation(webapp2.RequestHandler):
def post(self):
sentence = self.request.get('words')
# Google Translate API cannot handle strings > 100 characters
sentence = sentence[:100]
# Replace the non-alphanumeric characters
# The spaces in the sentence are replaced with the Plus symbol
sentence = urllib.urlencode({'q': sentence})
# Name of the MP3 file generated using the MD5 hash
mp3_file = hashlib.md5(sentence).hexdigest()
# Save the MP3 file in this folder with the .mp3 extension
mp3_file = mp3_file + ".mp3"
# Create the full URL
url = 'http://translate.google.com/translate_tts?ie=UTF-8&tl=en&' + sentence
# upload to blobstore
mp3_file = files.blobstore.create(mime_type = 'audio/mp3', _blobinfo_uploaded_filename = mp3_file)
mp3 = urllib.urlopen(url).read()
with files.open(mp3_file, 'a') as f:
f.write(mp3)
files.finalize(mp3_file)
blob_key = files.blobstore.get_blob_key(mp3_file)
logging.info('blob_key identified as %s', blob_key)
The problem has nothing to do with your code; it is correctly retrieving the data from the URL you gave.
For example, if I try this at the command line:
$ curl -O http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=revenues+in+new+york+were+56+million
I get a 1.5kB 403 error page, whose contents say:
403. That's an error.
Your client does not have permission to get URL /translate_tts?ie=UTF-8&tl=en&q=revenues+in+new+york+were+56+million from this server. (Client IP address: 1.2.3.4)
That’s all we know.
And your code does the exact same thing, whether run in GAE or directly in the interactive interpreter.
Most likely, the reason it works in your browser is that you do have permissions. So, what does that mean? It could mean that you have a valid SID cookie from google.com in your browser, but not your script. Or it could mean that your browser's user agent is recognized as something that can play HTML5 audio, but your script's isn't. Or…
Well, you can try to reverse-engineer what's different in the cookies, headers, etc. between your browser and your script, and narrow it down to the relevant difference, and use explicit headers or cookies or whatever you need to work around the problem.
But it will just break the next time Google changes anything.
And Google will probably not be happy with you if you try this. They offer a Google Translate API service that they want you to use, and they got rid of all of the free options for that API because of "substantial economic burden caused by extensive abuse." Trying to publish a Google App Engine web service that evades Google's API pricing by scraping their pages is probably not the kind of thing they enjoy their customers doing.