// here is working code for get token from google cloud
boolean lb_Success
string ls_refresh_token
string ls_url , ls_method, ls_ClientID, ls_Sercet, ls_scope, ls_redirect, ls_token_type
ls_url = "https://accounts.google.com/o/oauth2/token"
ls_method = "POST"
ls_ClientID = "your-client-id"
ls_Sercet = "client_Sercet"
string ls_project
ls_project = 'GOOGLEDRIVE'
ls_scope = "https://www.googleapis.com/auth/drive"
ls_redirect = "http://localhost"
// first to generate refreshtoekn i use function of_get_token
//return of_get_token (ls_project, ls_url , ls_method, ls_ClientID, ls_Sercet, ls_scope, ls_redirect, ls_token_type)
//function code
HttpClient lhc_Client
OAuthClient ioa_Client
TokenRequest itr_Request
TokenResponse itr_Response
//Get Token
string ls_AccessToken
String ls_Body, ls_type, ls_description, ls_uri, ls_state, ls_code, ls_Url_code
Integer li_rtn
CoderObject lco_Code
Blob lb_data
lhc_Client = Create HttpClient
lco_Code = Create CoderObject
If Not IsValid ( ioa_Client ) Then
ioa_Client = Create OAuthClient
End If
//url
If IsNull ( ls_Url ) Or Trim ( ls_Url ) = '' Then
MessageBox ( "Tips", "url is null!" )
Return ''
End If
itr_Request.tokenlocation = ls_Url
//Method
If IsNull ( ls_Method ) Or Trim ( ls_Method ) = '' Then
MessageBox ( "Tips", "Method is null!" )
Return ''
End If
itr_Request.Method = ls_Method
//client_id
If IsNull ( ls_id ) Or Trim ( ls_id ) = '' Then
MessageBox ( "Tips", "client_id is null!" )
Return ''
End If
itr_Request.clientid = ls_id
//client_secret
If IsNull ( ls_secret ) Or Trim ( ls_secret ) = '' Then
MessageBox ( "Error", "client_secret is null!" )
Return ''
End If
itr_Request.clientsecret = ls_secret
//scope
If Len ( ls_Scope ) > 0 Then
itr_Request.scope = ls_Scope
End If
//redirect
ls_Url_code = "https://accounts.google.com/o/oauth2/auth?client_id=" + ls_id + "&redirect_uri=" + ls_Redirect +"&scope=" + ls_Scope + "&response_type=code"
OpenWithParm ( w_webbrowser,ls_Url_code )
ls_code = Message.Stringparm
If Len ( ls_code ) < 1 Then Return ''
If Pos( ls_code, "code=" ) < 0 Then return ''
ls_code = Mid ( ls_code, pos(ls_code,"=") + 1)
lb_data = lco_Code.Urldecode( ls_code) //UrlDecode
ls_code = String ( lb_data,EncodingANSI!)
//granttype=authorization_code
itr_Request.granttype = "authorization_code"
itr_Request.ClearParams()
itr_Request.AppendParam("grant_type","authorization_code")
itr_Request.AppendParam( "client_id", ls_id )
itr_Request.AppendParam( "client_secret", ls_secret )
itr_Request.AppendParam( "scope", ls_Scope )
itr_Request.AppendParam( "code", ls_code )
itr_Request.AppendParam( "redirect_uri", ls_Redirect )
itr_Request.ClearHeaders()
itr_Request.SetHeader("Content-Type","application/x-www-form-urlencoded")
li_rtn = ioa_Client.AccessToken( itr_Request, itr_Response )
If li_rtn = 1 Then
li_rtn = itr_Response.GetBody(ls_Body)
If li_rtn = 1 Then
ls_AccessToken = itr_Response.GetAccessToken ( )
ls_token_type = itr_Response.Gettokentype( )
Else
MessageBox( "Error", "GetBody Return :" + String ( li_rtn ))
return ''
End If
else
MessageBox( "Error", "GetBody Return :" + String ( li_rtn ))
return ''
end if
string ls_refreshtoken
ls_refreshtoken = itr_Response.getrefreshtoken()
// save this refreshtoken in your database
If IsValid ( lco_Code ) Then Destroy ( lco_Code )
return ls_AccessToken
// to get token from refreshtoken
OAuthClient loac_Client
TokenRequest ltr_Request
TokenResponse ltr_Response
String ls_AccessToken, ls_Body, ls_type, ls_description, ls_uri, ls_state
Long ll_return
loac_Client = Create OAuthClient
ltr_Request.tokenlocation = "https://www.googleapis.com/oauth2/v4/token"
ltr_Request.Method = "POST"
ltr_Request.secureprotocol = 0
ltr_Request.granttype = "refresh_token"
ltr_Request.ClearParams()
ltr_Request.AppendParam("grant_type","refresh_token")
ltr_Request.AppendParam( "client_id", ls_ClientID )
ltr_Request.AppendParam( "client_secret", ls_Sercet )
ltr_Request.AppendParam( "refresh_token", ls_refresh_token )
ltr_Request.ClearHeaders()
ltr_Request.SetHeader("Content-Type","application/x-www-form-urlencoded")
ll_Return = loac_Client.AccessToken( ltr_Request, ltr_Response )
If ll_Return = 1 and ltr_Response.GetStatusCode () = 200 Then
ll_Return = ltr_Response.GetBody(ls_Body)
If ll_Return = 1 Then
ls_AccessToken = ltr_Response.GetAccessToken()
else
MessageBox( "AccessToken Falied", "Return :" + ls_AccessToken )
End If
Else
ll_Return = ltr_Response.GetTokenError(ls_type, ls_description, ls_uri, ls_state)
MessageBox( "AccessToken Falied", "Return :" + String ( ll_return ) + "~r~n" + ls_description )
End If
If IsValid ( loac_Client ) Then DesTroy ( loac_Client )
return ls_AccessToken
return ""