/*
* GoogleOAuth.prg
* Clase para conseguir autorización de Google a traves de un JSon de una ID de servicio Google
* Manual en https://www.chilkatsoft.com/refdoc/xChilkatAuthGoogleRef.html
* Ejemplos en https://www.example-code.com/vbscript/googleapi_access_token_json.asp
* Copyright 2016 Bingen
*
*/
#include "Xailer.ch"
DYNAMIC CreateObject
DYNAMIC XA_Break
//------------------------------------------------------------------------------
CLASS TGoogleOAuth FROM TComponent
DATA oChilkat
DATA oAuth
DATA oSocket
DATA oJSon
Data cToken Init ""
PUBLISHED:
PROPERTY cDLLName INIT "ChilkatAx.dll"
PROPERTY cActivexName INIT "Chilkat_9_5_0"
PROPERTY cKey INIT "xxxxxxxxxxx" Aquí tu clave de Chilkat
PROPERTY cUtility INIT "Autorización de Google"
PROPERTY cJsonFile INIT "" //Fichero que se obtiene desde https://console.developers.google.com/?hl=ES
PUBLIC:
DATA lInstalled INIT .F. READONLY
DATA nErrorCode INIT 0 READONLY
DATA cErrorDescription INIT "" READONLY
METHOD New( oParent ) CONSTRUCTOR // --> Self
METHOD Create( oParent ) CONSTRUCTOR // --> Self
METHOD Close() // --> Nil
METHOD Free() // --> Nil
METHOD GetAuth()
PRIVATE:
ENDCLASS
//------------------------------------------------------------------------------
METHOD New( oParent ) CLASS TGoogleOAuth
//Ver si está instalada la DLL de ChilKat
TRY
::oChilkat := TOleAuto():New( ::cActivexName+".Global" )
IF ValType( ::oChilkat:Version ) == "C"
::lInstalled := .T.
ENDIF
CATCH
::lInstalled := .F.
END
//Si no esta instalada y no hay DLL
If !::lInstalled .And. !File(::cDllName)
MsgError("No se ha localizado el archivo necesario para "+::cUtility+" "+::cDLLName+CRLF+CRLF+"Contacte con el distribuidor de la aplicación para poder utilizar "+::cUtility,"Error envío "+::cUtility+" 1")
Return .F.
Endif
//Si no esta instalada y si hay DLL intentar registrarla
If !::lInstalled .And. File(::cDLLName)
If !DLLRegisterServer( ::cDLLName )
MsgError("No se ha podido registrar el archivo necesario para "+::cUtility+" "+::cDLLName+CRLF+CRLF+"Debería de ejecutar la aplicación con permisos de administrador una sola vez para poder activar "+::cUtility,"Error envío "+::cUtility+" 2")
Return .F.
Else
//Ver de nuevo si está instalada la DLL de ChilKat
TRY
::oChilkat := TOleAuto():New( ::cActivexName+".Global" )
IF ValType( ::oChilkat:Version ) == "C"
::lInstalled := .T.
ENDIF
CATCH
::lInstalled := .F.
END
Endif
Endif
If !::lInstalled
MsgError("No se ha podido poner en marcha el archivo necesario para "+::cUtility+" "+::cDLLName+CRLF+CRLF+"Contacte con el distribuidor de la aplicación para poder utilizar "+::cUtility,"Error envío "+::cUtility+" 3")
Return .F.
Endif
//Si está instalada meterle la clave de ChilKat
IF ::oChilkat:UnlockBundle(::cKey) <> 1
MsgError("No se ha podido poner en marcha el archivo necesario para "+::cUtility+" "+::cDLLName+CRLF+CRLF+"Contacte con el distribuidor de la aplicación para poder utilizar "+::cUtility,"Error envío "+::cUtility+" 4")
Return .F.
ENDIF
::oChilkat := Nil
::Create( oParent )
RETURN Self
//------------------------------------------------------------------------------
METHOD Create( oParent ) CLASS TGoogleOAuth
::Super:Create( oParent )
::oAuth := TOleAuto():New( ::cActivexName+".AuthGoogle" )
RETURN Self
//------------------------------------------------------------------------------
METHOD Close() CLASS TGoogleOAuth
::Free()
RETURN Nil
//------------------------------------------------------------------------------
METHOD Free() CLASS TGoogleOAuth
::oAuth := Nil
::oSocket := Nil
::Super:Free()
RETURN Nil
//------------------------------------------------------------------------------
METHOD GetAuth() CLASS TGoogleOAuth
Local cJsonKey:="", lOk:=.F.
IF !File(::cJsonFile)
MsgError("Error no se ha suministrado el archivo JSon para Google oAuth",::cUtility)
Return .F.
Endif
::oJson := TOleAuto():New( ::cActivexName+".FileAccess" )
cJsonKey:=::oJSon:ReadEntireTextFile(::cJsonFile,"utf-8")
If ::oJSon:LastMethodSuccess <> 1
MsgError("Error el archivo JSon suministrado para Google oAuth no se ha podido cargar",::cUtility)
Return .F.
Endif
::oAuth:JsonKey = cJsonKey
::oAuth:emailaddress:="bingen@bisoft.es"
::oAuth:Scope = "https://www.googleapis.com/auth/calendar" // Puede ser también "https://www.googleapis.com/auth/calendar.readonly"
::oAuth:ExpireNumSeconds = 3600
::oSocket := TOleAuto():New( ::cActivexName+".Socket" )
If ::oSocket:Connect("www.googleapis.com",443,1,5000)<>1
MsgError("Error 1 de conexión a socket "+CRLF+ChilkatError(::oSocket:LastErrorText,::cKey),::cUtility)
Return .F.
EndIf
//Conectar a Google
If ::oSocket:Connect("www.googleapis.com",443,1,5000)<>1
MsgError("Error 2 de conexión a socket "+CRLF+ChilkatError(::oSocket:LastErrorText,::cKey),::cUtility)
Return .F.
EndIf
//Obtener el Token
If ::oAuth:ObtainAccessToken(::oSocket)<>1
MsgError("Error de petición de autorización "+CRLF+ChilkatError(::oAuth:LastErrorText,::cKey),::cUtility)
Return .F.
EndIf
::cToken:=::oAuth:AccessToken
RETURN .T.
//------------------------------------------------------------------------------
/* Sample
With Object TGoogleOAuth():New( Self )
:cJsonFile:="client_secret_706301693148-mr8vb35chsb02svrsaglabhb4br4.apps.googleusercontent.com.json"
If :Version=Nil
MsgInfo(:cErrorDescription,:nErrorCode )
Endif
END
*/