@@ -4,7 +4,6 @@ import type {
4
4
} from '@crawlee/core' ;
5
5
import { ProxyConfiguration as CoreProxyConfiguration } from '@crawlee/core' ;
6
6
import { gotScraping } from '@crawlee/utils' ;
7
- import type { UserProxy } from 'apify-client' ;
8
7
import ow from 'ow' ;
9
8
10
9
import { APIFY_ENV_VARS , APIFY_PROXY_VALUE_REGEX } from '@apify/consts' ;
@@ -260,7 +259,27 @@ export class ProxyConfiguration extends CoreProxyConfiguration {
260
259
*/
261
260
async initialize ( ) : Promise < boolean > {
262
261
if ( this . usesApifyProxy ) {
263
- await this . _setPasswordIfToken ( ) ;
262
+ if ( ! this . password ) {
263
+ await this . _setPasswordIfToken ( ) ;
264
+ }
265
+
266
+ if ( ! this . password ) {
267
+ if ( Actor . isAtHome ( ) ) {
268
+ throw new Error (
269
+ `Apify Proxy password must be provided using options.password or the "${ APIFY_ENV_VARS . PROXY_PASSWORD } " environment variable. ` +
270
+ `You can also provide your Apify token via the "${ APIFY_ENV_VARS . TOKEN } " environment variable, ` +
271
+ `so that the SDK can fetch the proxy password from Apify API, when ${ APIFY_ENV_VARS . PROXY_PASSWORD } is not defined` ,
272
+ ) ;
273
+ } else {
274
+ this . log . warning (
275
+ `No proxy password or token detected, running without proxy. To use Apify Proxy locally, ` +
276
+ `provide options.password or "${ APIFY_ENV_VARS . PROXY_PASSWORD } " environment variable. ` +
277
+ `You can also provide your Apify token via the "${ APIFY_ENV_VARS . TOKEN } " environment variable, ` +
278
+ `so that the SDK can fetch the proxy password from Apify API, when ${ APIFY_ENV_VARS . PROXY_PASSWORD } is not defined` ,
279
+ ) ;
280
+ }
281
+ }
282
+
264
283
return this . _checkAccess ( ) ;
265
284
}
266
285
@@ -415,55 +434,23 @@ export class ProxyConfiguration extends CoreProxyConfiguration {
415
434
}
416
435
417
436
/**
418
- * Checks if Apify Token is provided in env and gets the password via API and sets it to env
437
+ * Fetch & set the proxy password from Apify API if an Apify token is provided.
419
438
*/
439
+ // TODO: Make this private
420
440
protected async _setPasswordIfToken ( ) : Promise < void > {
421
441
const token = this . config . get ( 'token' ) ;
422
442
423
- if ( token ) {
424
- let proxy : UserProxy ;
425
-
426
- try {
427
- const user = await Actor . apifyClient . user ( ) . get ( ) ;
428
- proxy = user . proxy ! ;
429
- } catch ( error ) {
430
- if ( Actor . isAtHome ( ) ) {
431
- throw error ;
432
- } else {
433
- this . log . warning (
434
- `Failed to fetch user data based on token, disabling proxy.` ,
435
- { error } ,
436
- ) ;
437
- return ;
438
- }
439
- }
440
-
441
- const { password } = proxy ! ;
442
-
443
- if ( this . password ) {
444
- if ( this . password !== password ) {
445
- this . log . warning (
446
- 'The Apify Proxy password you provided belongs to' +
447
- ' a different user than the Apify token you are using. Are you sure this is correct?' ,
448
- ) ;
449
- }
450
- } else {
451
- this . password = password ;
452
- }
453
- }
454
-
455
- if ( ! this . password ) {
443
+ if ( ! token ) return ;
444
+ try {
445
+ const user = await Actor . apifyClient . user ( ) . get ( ) ;
446
+ this . password = user . proxy ?. password ;
447
+ } catch ( error ) {
456
448
if ( Actor . isAtHome ( ) ) {
457
- throw new Error (
458
- `Apify Proxy password must be provided using options.password or the "${ APIFY_ENV_VARS . PROXY_PASSWORD } " environment variable. ` +
459
- `If you add the "${ APIFY_ENV_VARS . TOKEN } " environment variable, the password will be automatically inferred.` ,
460
- ) ;
449
+ throw error ;
461
450
} else {
462
- this . log . warning (
463
- `No proxy password or token detected, running without proxy. To use Apify Proxy locally, ` +
464
- `provide options.password or "${ APIFY_ENV_VARS . PROXY_PASSWORD } " environment variable. ` +
465
- `If you add the "${ APIFY_ENV_VARS . TOKEN } " environment variable, the password will be automatically inferred.` ,
466
- ) ;
451
+ this . log . warning ( `Failed to fetch user data using token` , {
452
+ error,
453
+ } ) ;
467
454
}
468
455
}
469
456
}
0 commit comments