1. ritesh desai
  2. PowerBuilder
  3. Saturday, 24 November 2018 11:18 AM UTC

Pl. Guide me how to upload file to google drive using oauth2 token.

thanks

 

ritesh desai Accepted Answer Pending Moderation
  1. Friday, 16 October 2020 09:48 AM UTC
  2. PowerBuilder
  3. # 1

// 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 ""

Comment
  1. Armeen Mazda @Appeon
  2. Friday, 16 October 2020 14:27 PM UTC
Thanks for sharing!
  1. Helpful
There are no comments made yet.
Armeen Mazda @Appeon Accepted Answer Pending Moderation
  1. Thursday, 15 October 2020 20:40 PM UTC
  2. PowerBuilder
  3. # 2

Maybe it will help to watch Kevin's Webinar and code sample about connecting to Dropbox.

At a high level, you need to use the OAuthClient and RESTClient.

Comment
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Thursday, 15 October 2020 20:27 PM UTC
  2. PowerBuilder
  3. # 3
Comment
There are no comments made yet.
David Canicoba Accepted Answer Pending Moderation
  1. Thursday, 15 October 2020 19:50 PM UTC
  2. PowerBuilder
  3. # 4

I mean Google Drive api.

Comment
There are no comments made yet.
David Canicoba Accepted Answer Pending Moderation
  1. Thursday, 15 October 2020 19:49 PM UTC
  2. PowerBuilder
  3. # 5

My friend can hoy send me something about google drive año in Powerbuilder. My email is neocani@gmail.com,  please.

Comment
There are no comments made yet.
Kevin Ridley Accepted Answer Pending Moderation
  1. Tuesday, 27 November 2018 13:23 PM UTC
  2. PowerBuilder
  3. # 6

At a high level, here's what you would do:

Using the OAuthClient, you request an access token from the Google Servers, then you use the token to make api requests.  First you setup a clientid from the console, or it looks like you can use a service account, which I'd recommend if you can get it to work.  The basics are - you setup a TokenRequest and send that using OauthClient.AccessToken(tokenrequest, tokenresponse).  If you don't use the service account, this is sometimes a 2 step process where you first get an authorization code, then you use the authorization code to get the token.  Either way, you check the tokenresponse.GetStatusCode() (should be something in the 200 range for success) and then tokenresponse..GetAccessToken()

 

You will use this access token for all requests to the API during a session, so store it in a variable or the db.  Some providers will set a timeout on access tokens, and also supply a refresh token that you can use to get a new access token.  When you make the REST calls using the token, you can use either continue to use the OauthClient, or the HTTPClient, possibly even the RESTClient, depending on the resultset.  Here's some hopefully helpful links.

 

https://developers.google.com/identity/protocols/OAuth2

https://developers.google.com/identity/protocols/OAuth2UserAgent

https://console.developers.google.com/apis/library/drive.googleapis.com?id=e44a1596-da14-427c-9b36-5eb6acce3775

 

 

I did something similar for my Elevate presentation for using OAuth to do REST api calls against Microsoft Dynamics.  Hopefully they will be posting the code soon, if not I can send you something or post here.

 

Good luck,

Kevin

Comment
  1. Kevin Ridley
  2. Tuesday, 27 November 2018 13:27 PM UTC
It looks like you would use the drive.files.create API

https://developers.google.com/apis-explorer/?hl=en_US#p/drive/v3/
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.