@@ -5,6 +5,29 @@ const { GoogleAuth, OAuth2Client } = require('google-auth-library');
55var fs = require ( "fs" ) ;
66const request = require ( 'request' ) ;
77
8+ function GetGoogleAuthClient ( config : Igoogleauth_credentials ) : any {
9+ if ( config != null ) {
10+ if ( typeof config . serviceaccount === "string" && ! NoderedUtil . IsNullEmpty ( config . serviceaccount ) ) {
11+ config . serviceaccount = JSON . parse ( config . serviceaccount ) ;
12+ }
13+ if ( ! NoderedUtil . IsNullEmpty ( config . serviceaccount ) ) {
14+ this . auth = new GoogleAuth ( {
15+ scopes : config . scopes ,
16+ credentials : config . serviceaccount
17+ } ) ;
18+ }
19+ if ( ! NoderedUtil . IsNullEmpty ( config . clientid ) || ! NoderedUtil . IsNullEmpty ( config . clientsecret ) || ! NoderedUtil . IsNullEmpty ( config . redirecturi ) ) {
20+ this . Client = new OAuth2Client (
21+ config . clientid ,
22+ config . clientsecret ,
23+ config . redirecturi
24+ ) ;
25+ if ( ! NoderedUtil . IsNullEmpty ( config . tokens ) ) {
26+ this . Client . setCredentials ( JSON . parse ( config . tokens ) ) ;
27+ }
28+ }
29+ }
30+ }
831
932export interface Igoogleauth_credentials {
1033 name : string ;
@@ -166,41 +189,14 @@ export class googleauth_request {
166189 this . _config = RED . nodes . getNode ( this . config . config ) ;
167190 this . method = this . config . method ;
168191 this . url = this . config . url ;
169- this . init ( ) ;
192+ this . Client = GetGoogleAuthClient ( this . _config ) ;
170193 this . node . on ( 'input' , this . oninput ) ;
171- } catch ( error ) {
172- NoderedUtil . HandleError ( this , error ) ;
173- }
174- }
175- init ( ) {
176- try {
177- if ( this . _config != null ) {
178- if ( typeof this . _config . serviceaccount === "string" && ! NoderedUtil . IsNullEmpty ( this . _config . serviceaccount ) ) {
179- this . _config . serviceaccount = JSON . parse ( this . _config . serviceaccount ) ;
180- }
181- if ( ! NoderedUtil . IsNullEmpty ( this . _config . serviceaccount ) ) {
182- this . auth = new GoogleAuth ( {
183- scopes : this . _config . scopes ,
184- credentials : this . _config . serviceaccount
185- } ) ;
186- }
187- if ( ! NoderedUtil . IsNullEmpty ( this . _config . clientid ) || ! NoderedUtil . IsNullEmpty ( this . _config . clientsecret ) || ! NoderedUtil . IsNullEmpty ( this . _config . redirecturi ) ) {
188- this . Client = new OAuth2Client (
189- this . _config . clientid ,
190- this . _config . clientsecret ,
191- this . _config . redirecturi
192- ) ;
193- if ( ! NoderedUtil . IsNullEmpty ( this . _config . tokens ) ) {
194- this . Client . setCredentials ( JSON . parse ( this . _config . tokens ) ) ;
195- }
196- }
197- }
198194 this . node . status ( { } ) ;
199195 } catch ( error ) {
200196 NoderedUtil . HandleError ( this , error ) ;
201-
202197 }
203198 }
199+
204200 async oninput ( msg : any , send : any , done : any ) {
205201 try {
206202 this . node . status ( { fill : "blue" , shape : "dot" , text : "Getting client" } ) ;
0 commit comments