01. Exemplo

Exemplo de uso da Classe FWoAuth2Client com Client para Google Drive.


#include "protheus.ch"

//-------------------------------------------------------------------
/*/{Protheus.doc} gDrive
Função de teste de consumo de oAuth2 do Google Drive

@author Daniel Mendes
@since 08/09/2022
@version 1.0
/*/
//-------------------------------------------------------------------
user Function gDrive()
local cAppKey as character
local cAppSecret as character
local oAuth as object
local cResult as character

cAppKey	:= "MinhaChaveDeAplicação"
cAppSecret := "MeuSegredoDeAplicação"

oAuth := FWGoogleDrive():New(cAppKey, cAppSecret)
oAuth:SetScopes({"https://www.googleapis.com/auth/drive"})

cResult := oAuth:Access("https://www.googleapis.com/drive/v3/files")

MsgInfo(cResult, "FWoAuth2Client - Resultado")

oAuth:Destroy()
FreeObj(oAuth)

return

//-------------------------------------------------------------------
/*/{Protheus.doc} FWGoogleDrive
Classe de consumo do oAuth2 do Google Drive

@author Daniel Mendes
@since 08/09/2022
@version 1.0
/*/
//-------------------------------------------------------------------
class FWGoogleDrive from FWoAuth2Client
	data aScopes as array

	method New() constructor
	method Destroy()
	method ClassName()
	method SetScopes()
endclass

//-------------------------------------------------------------------
/*/{Protheus.doc} New
Método construtor

@param cConsumer, character, chave da aplicação
@param cSecret, character, segredo da aplicação

@return self, object, instância da classe

@author Daniel Mendes
@since 08/09/2022
@version 1.0
/*/
//-------------------------------------------------------------------
Method New(cConsumer, cSecret) Class FWGoogleDrive
Local cAuth_uri as character
Local cToken_uri as character
Local oURL as object

cAuth_uri  := "https://accounts.google.com/o/oauth2/auth"
cToken_uri := "https://oauth2.googleapis.com/token"
oURL      := FwoAuth2Url():New(cAuth_uri, cToken_uri)

_Super:New(cConsumer, cSecret, oURL)

::SetGrantInUrl(.F.)
::SetAuthInHeader(.F.)
::SetScopes({"https://www.googleapis.com/auth/drive"})
::SetAsCode()
::SetQueryAuthorization()

return self

//-------------------------------------------------------------------
/*/{Protheus.doc} Destroy
Método destrutor da classe

@author Daniel Mendes
@since 08/09/2022
@version 1.0
/*/
//-------------------------------------------------------------------
method Destroy() Class FWGoogleDrive
::aScopes := aSize(::aScopes, 0)
return _Super:Destroy()

//-------------------------------------------------------------------
/*/{Protheus.doc} ClassName
Retorna o nome da classe

@return character

@author Daniel Mendes
@since 08/09/2022
@version 1.0
/*/
//-------------------------------------------------------------------
method ClassName() Class FWGoogleDrive
return "FWGoogleDrive"
 
//-------------------------------------------------------------------
/*/{Protheus.doc} SetScopes
Efetua o set dos scopes

@param aScopes, array, scopes

@return logical, indica se o Set foi efetuado corretamente

@author Daniel Mendes
@since 08/09/2022
@version 1.0
/*/
//-------------------------------------------------------------------
method SetScopes(aScopes) Class FWGoogleDrive
local cScope as character
local nI as numeric

default aScopes := {}

::aScopes := aScopes

for nI := 1 to Len(aScopes)
	if nI == 1
		cScope := "scope="+aScopes[nI]
	else
		cScope += "," + aScopes[nI]
	endif
next

::SetAuthOptions(cScope)

return Len(aScopes) > 0