2014년 12월 26일 금요일

How to get Acces Token and refresh token for Fusion Api


I want build app using fusiontable as DB. And without FusionT rotating icon and more useful so ı need use oauth.

I know Taifun's do it but its premium content

I need oauth verify for insert update or other actions

How can ı get acces token and refresh token 

#2. Your web application redirects the user to Google Authorization page
#3. User grants your web application access
print 'Visit the URL below in a browser to authorize'
print '%s?client_id=%s&redirect_uri=%s&scope=%s&response_type=code' % \
  ('https://accounts.google.com/o/oauth2/auth',
  client_id,
  redirect_uri,
  'https://www.googleapis.com/auth/fusiontables')
#4. Google redirects the user back to your web application and
#   returns an authorization code
auth_code = raw_input('Enter authorization code (parameter of URL): ')
#5. Your application requests an access token and refresh token from Google
data = urllib.urlencode({
  'code': auth_code,
  'client_id': client_id,
  'client_secret': client_secret,
  'redirect_uri': redirect_uri,
  'grant_type': 'authorization_code'
})
request = urllib2.Request(
  url='https://accounts.google.com/o/oauth2/token',
  data=data)
request_open = urllib2.urlopen(request)
#6. Google returns access token, refresh token, and expiration of
#   access token
response = request_open.read()
request_open.close()
tokens = json.loads(response)
access_token = tokens['access_token']
refresh_token = tokens['refresh_token']
#7. Access token can be used for all subsequent requests to Fusion Tables,
#   until the token expires
request = urllib2.Request(
  url='https://www.google.com/fusiontables/api/query?%s' % \
    (urllib.urlencode({'access_token': access_token,
                       'sql': 'SELECT * FROM 123456'})))
request_open = urllib2.urlopen(request)
response = request_open.read()
request_open.close()
print response
#8. When the access token expires,
#   the refresh token is used to request a new access token
data = urllib.urlencode({
  'client_id': client_id,
  'client_secret': client_secret,
  'refresh_token': refresh_token,
  'grant_type': 'refresh_token'})
request = urllib2.Request(
  url='https://accounts.google.com/o/oauth2/token',
  data=data)
request_open = urllib2.urlopen(request)
response = request_open.read()
request_open.close()
tokens = json.loads(response)
access_token = tokens['access_token']

How to apply it to app inventor 

Can someone help me?



well, my fusionDB example is not premium content, it's only a "No free lunch" example and you can get it for a small fee...

as a start you can read the Google documentation about authentication https://developers.google.com/accounts/docs/OAuth2 and try to convert this into App Inventor
Good luck!



I can get acces token but ı cant send post how to do it?

POST /oauth2/v3/token HTTP/1.1
Host: www.googleapis.comContent-Type: application/x-www-form-urlencoded

code=4/v6xr77ewYqhvHSyW6UJ1w7jKwAzu&
client_id=8819981768.apps.googleusercontent.com&
client_secret=your_client_secret&
redirect_uri=https://oauth2-login-demo.appspot.com/code&
grant_type=authorization_code
I try 
 token block.png





Thanks

I do it i use pagetitle for accestoken and i use segment block for refreshT

I use start number 172 and leght number 45 for segment block so ı can get refresh token 

I have litle question about refresh token leght is it always 45?


댓글 없음:

댓글 쓰기