@@ -63,36 +63,44 @@ feature {NONE} -- Initialization
6363 end
6464
6565 get_token_from_url : OAUTH_TOKEN
66+ require
67+ attached api_key
68+ attached api_secret
6669 local
6770 google : OAUTH_ 20 _GOOGLE_API
6871 config : OAUTH_CONFIG
6972 api_service : OAUTH_SERVICE_I
7073 file : FILE
7174 do
72- create Result .make_empty
73- create config .make_default (api_key , api_secret )
74- config .set_callback (" urn:ietf:wg:oauth:2.0:oob" )
75- config .set_scope (" https://www.googleapis.com/auth/spreadsheets" )
76- create google
77- api_service := google .create_service (config )
78- print (" %N===Google OAuth Workflow ===%N" )
79-
80- -- Obtain the Authorization URL
81- print (" %NFetching the Authorization URL..." );
82- if attached api_service .authorization_url (empty_token ) as lauthorization_url then
83- print (" %NGot the Authorization URL!%N" );
84- print (" %NNow go and authorize here:%N" );
85- print (lauthorization_url );
86- print (" %NAnd paste the authorization code here%N" );
87- io .read_line
88- end
75+ check
76+ attached api_key as l_api_key
77+ attached api_secret as l_api_secret
78+ then
79+ create Result .make_empty
80+ create config .make_default (l_api_key , l_api_secret )
81+ config .set_callback (" urn:ietf:wg:oauth:2.0:oob" )
82+ config .set_scope (" https://www.googleapis.com/auth/spreadsheets" )
83+ create google
84+ api_service := google .create_service (config )
85+ print (" %N===Google OAuth Workflow ===%N" )
86+
87+ -- Obtain the Authorization URL
88+ print (" %NFetching the Authorization URL..." );
89+ if attached api_service .authorization_url (empty_token ) as lauthorization_url then
90+ print (" %NGot the Authorization URL!%N" );
91+ print (" %NNow go and authorize here:%N" );
92+ print (lauthorization_url );
93+ print (" %NAnd paste the authorization code here%N" );
94+ io .read_line
95+ end
8996
90- if attached api_service .access_token_post (empty_token , create {OAUTH_VERIFIER }.make (io .last_string )) as access_token then
91- create {PLAIN_TEXT_FILE } file .make_create_read_write (" token.access" )
92- file .put_string (serialize (access_token ))
93- Result := access_token
94- file .flush
95- file .close
97+ if attached api_service .access_token_post (empty_token , create {OAUTH_VERIFIER }.make (io .last_string )) as access_token then
98+ create {PLAIN_TEXT_FILE } file .make_create_read_write (" token.access" )
99+ file .put_string (serialize (access_token ))
100+ Result := access_token
101+ file .flush
102+ file .close
103+ end
96104 end
97105 end
98106
@@ -102,32 +110,82 @@ feature {NONE} -- Initialization
102110 -- client_secret=your_client_secret&
103111 -- refresh_token=refresh_token&
104112 -- grant_type=refresh_token
113+ require
114+ attached api_key
115+ attached api_secret
105116 local
106117 google : OAUTH_ 20 _GOOGLE_API
107118 config : OAUTH_CONFIG
108119 request : OAUTH_REQUEST
109120 file : FILE
110121 api : OAUTH_ 20 _API
111122 do
112- create Result .make_empty
113- create google
114- create request .make (" POST" , google .access_token_endpoint )
115- request .add_body_parameter (" client_id" , api_key )
116- request .add_body_parameter (" client_secret" , api_secret )
117- request .add_body_parameter (" refresh_token" , if attached a_token .refresh_token as l_token then l_token else " " end )
118- request .add_body_parameter (" grant_type" , " refresh_token" )
119- if attached request .execute as l_response then
120- if attached l_response .body as l_body then
121- if attached {OAUTH_TOKEN } google .access_token_extractor .extract (l_body ) as l_access_token then
122- create {PLAIN_TEXT_FILE } file .make_create_read_write (" token.access" )
123- file .put_string (serialize (l_access_token ))
124- Result := l_access_token
125- file .flush
126- file .close
123+ check
124+ attached api_key as l_api_key
125+ attached api_secret as l_api_secret
126+ then
127+ create Result .make_empty
128+ create google
129+ create request .make (" POST" , google .access_token_endpoint )
130+ request .add_body_parameter (" client_id" , l_api_key )
131+ request .add_body_parameter (" client_secret" , l_api_secret )
132+ request .add_body_parameter (" refresh_token" , if attached a_token .refresh_token as l_token then l_token else " " end )
133+ request .add_body_parameter (" grant_type" , " refresh_token" )
134+ if attached request .execute as l_response then
135+ if attached l_response .body as l_body then
136+ if attached {OAUTH_TOKEN } google .access_token_extractor .extract (l_body ) as l_access_token then
137+ create {PLAIN_TEXT_FILE } file .make_create_read_write (" token.access" )
138+ file .put_string (serialize (l_access_token ))
139+ Result := l_access_token
140+ file .flush
141+ file .close
142+ end
127143 end
128144 end
129145 end
130146 end
147+
148+ feature -- Status Setting
149+
150+ set_from_json_credentials_file_path (an_fp : PATH )
151+ -- sets api_key and api_secret from given api credentials file path normally provided by google -> https://console.developers.google.com
152+ -- create a Create OAuth client ID -> desktop app -> and export it to a json file with download link
153+ local
154+ l_json_parser : JSON_PARSER
155+ l_ut : FILE_UTILITIES
156+ do
157+ check
158+ l_ut .file_exists (an_fp .utf_ 8 _name )
159+ could_read_json_credentials_file : attached (create {JSON_FILE_READER }).read_json_from (an_fp .utf_ 8 _name ) as l_json_file_content
160+ then
161+ create l_json_parser .make_with_string (l_json_file_content )
162+ l_json_parser .parse_content
163+ check
164+ valid_json_file_content : l_json_parser .is_valid
165+ then
166+ check
167+ valid_main_object : attached {JSON_OBJECT } l_json_parser .parsed_json_value as l_main_json_o
168+ then
169+ check
170+ valid_installed : attached {JSON_OBJECT } l_main_json_o .item (" installed" ) as l_installed_jso
171+ then
172+ check
173+ has_client_id : attached {JSON_STRING } l_installed_jso .item (" client_id" ) as l_client_id_js_s
174+ then
175+ api_key := l_client_id_js_s .unescaped_string_ 8
176+ end
177+
178+ check
179+ has_client_secret : attached {JSON_STRING } l_installed_jso .item (" client_secret" ) as l_client_secret_js_s
180+ then
181+ api_secret := l_client_secret_js_s .unescaped_string_ 8
182+ end
183+ end
184+ end
185+ end
186+ end
187+ end
188+
131189feature -- Serialize Access Token
132190
133191 serialize (a_object : ANY ): STRING
@@ -180,7 +238,7 @@ feature -- Token
180238
181239feature {NONE } -- Implementation
182240
183- api_key : STRING = " 546059329455-.. "
184- api_secret : STRING = " i45lTLSjal... "
241+ api_key : detachable STRING
242+ api_secret : detachable STRING
185243 empty_token : detachable OAUTH_TOKEN
186244end
0 commit comments