In this article we will learn about some of the frequently asked GO programming questions in technical like “documentation for https://www.googleapis.com/oauth2/v4/token” Code Answer. When creating scripts and web applications, error handling is an important part. If your code lacks error checking code, your program may look very unprofessional and you may be open to security risks. Error or stack handling on go was simple and easy. An error message with filename, line number and a message describing the error is sent to the browser. This tutorial contains some of the most common error checking methods in GO. Below are some solution about “documentation for https://www.googleapis.com/oauth2/v4/token” Code Answer.
documentation for https://www.googleapis.com/oauth2/v4/token
xxxxxxxxxx
1
>>> # Credentials you get from registering a new application
2
>>> client_id = '<the id you get from google>.apps.googleusercontent.com'
3
>>> client_secret = '<the secret you get from google>'
4
>>> redirect_uri = 'https://your.registered/callback'
5
6
>>> # OAuth endpoints given in the Google API documentation
7
>>> authorization_base_url = "https://accounts.google.com/o/oauth2/v2/auth"
8
>>> token_url = "https://www.googleapis.com/oauth2/v4/token"
9
>>> scope = [
10
"https://www.googleapis.com/auth/userinfo.email",
11
"https://www.googleapis.com/auth/userinfo.profile"
12
]
13
14
>>> from requests_oauthlib import OAuth2Session
15
>>> google = OAuth2Session(client_id, scope=scope, redirect_uri=redirect_uri)
16
17
>>> # Redirect user to Google for authorization
18
>>> authorization_url, state = google.authorization_url(authorization_base_url,
19
# offline for refresh token
20
# force to always make user click authorize
21
access_type="offline", prompt="select_account")
22
>>> print 'Please go here and authorize,', authorization_url
23
24
>>> # Get the authorization verifier code from the callback url
25
>>> redirect_response = raw_input('Paste the full redirect URL here:')
26
27
>>> # Fetch the access token
28
>>> google.fetch_token(token_url, client_secret=client_secret,
29
authorization_response=redirect_response)
30
31
>>> # Fetch a protected resource, i.e. user profile
32
>>> r = google.get('https://www.googleapis.com/oauth2/v1/userinfo')
33
>>> print r.content
34