Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

Apify.call() was causing permissions error

Jan Curnf57abce2e161
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 353ba8381..a3a75f999 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+xxx
+==================
+- Bugfix: `Apify.call()` was causing permissions error.
+
0.9.4 / 2018-11-19
==================
- Automatically adding `--enable-resource-load-scheduler=false`
diff --git a/src/actor.js b/src/actor.js
index 131d41e81..5dd5070c9 100644
--- a/src/actor.js
+++ b/src/actor.js
@@ -41,23 +41,27 @@ const waitForRunToFinish = async ({ actId, runId, token, waitSecs }) => {
return true;
};
+ const getRunOpts = { actId, runId };
+ if (token) getRunOpts.token = token;
+
while (shouldRepeat()) {
- const waitForFinish = waitSecs
+ getRunOpts.waitForFinish = waitSecs
? Math.round(waitSecs - (Date.now() - startedAt) / 1000)
: 999999;
- updatedRun = await acts.getRun({ actId, runId, token, waitForFinish });
+ updatedRun = await acts.getRun(getRunOpts);
// It might take some time for database replicas to get up-to-date,
// so getRun() might return null. Wait a little bit and try it again.
if (!updatedRun) await Promise.delay(250);
}
- if (!updatedRun) throw new ApifyCallError({ id: runId, actId }, 'Apify.call() failed, cannot fetch run from a server');
- if (
- updatedRun.status !== ACT_JOB_STATUSES.SUCCEEDED
- && updatedRun.status !== ACT_JOB_STATUSES.RUNNING
- ) throw new ApifyCallError(updatedRun);
+ if (!updatedRun) {
+ throw new ApifyCallError({ id: runId, actId }, 'Apify.call() failed, cannot fetch run from a server');
+ }
+ if (updatedRun.status !== ACT_JOB_STATUSES.SUCCEEDED && updatedRun.status !== ACT_JOB_STATUSES.RUNNING) {
+ throw new ApifyCallError(updatedRun);
+ }
return updatedRun;
};
@@ -313,10 +317,13 @@ export const call = async (actId, input, options = {}) => {
// RunAct() options.
const { build, memoryMbytes, timeoutSecs } = optionsCopy;
- const runActOpts = {};
+ const runActOpts = {
+ actId,
+ };
checkParamOrThrow(build, 'build', 'Maybe String');
checkParamOrThrow(memoryMbytes, 'memoryMbytes', 'Maybe Number');
checkParamOrThrow(timeoutSecs, 'timeoutSecs', 'Maybe Number');
+ if (token) runActOpts.token = token;
if (build) runActOpts.build = build;
if (memoryMbytes) runActOpts.memory = memoryMbytes;
if (timeoutSecs >= 0) runActOpts.timeout = timeoutSecs; // Zero is valid value!
@@ -333,7 +340,7 @@ export const call = async (actId, input, options = {}) => {
// Run actor.
const { waitSecs } = options;
checkParamOrThrow(waitSecs, 'waitSecs', 'Maybe Number');
- const run = await acts.runAct(Object.assign({ actId, token }, runActOpts));
+ const run = await acts.runAct(runActOpts);
if (waitSecs <= 0) return run; // In this case there is nothing more to do.
// Wait for run to finish.
@@ -417,7 +424,9 @@ export const callTask = async (taskId, input, options = {}) => {
// Run task.
const { waitSecs } = options;
checkParamOrThrow(waitSecs, 'waitSecs', 'Maybe Number');
- const run = await tasks.runTask({ taskId, token });
+ const opts = { taskId };
+ if (token) opts.token = token;
+ const run = await tasks.runTask(opts);
if (waitSecs <= 0) return run; // In this case there is nothing more to do.
// Wait for run to finish.
diff --git a/test/actor.js b/test/actor.js
index 7321d4df0..555094614 100644
--- a/test/actor.js
+++ b/test/actor.js
@@ -297,15 +297,15 @@ describe('Apify.call()', () => {
const actsMock = sinon.mock(Apify.client.acts);
actsMock.expects('runAct')
- .withExactArgs({ actId, token: undefined })
+ .withExactArgs({ actId })
.once()
.returns(Promise.resolve(runningRun));
actsMock.expects('getRun')
- .withExactArgs({ actId, runId: run.id, waitForFinish: 999999, token: undefined })
+ .withExactArgs({ actId, runId: run.id, waitForFinish: 999999 })
.once()
.returns(Promise.resolve(runningRun));
actsMock.expects('getRun')
- .withExactArgs({ actId, runId: run.id, waitForFinish: 999999, token: undefined })
+ .withExactArgs({ actId, runId: run.id, waitForFinish: 999999 })
.once()
.returns(Promise.resolve(finishedRun));
@@ -338,15 +338,15 @@ describe('Apify.call()', () => {
const actsMock = sinon.mock(Apify.client.acts);
actsMock.expects('runAct')
- .withExactArgs({ actId, token: undefined })
+ .withExactArgs({ actId })
.once()
.returns(Promise.resolve(runningRun));
actsMock.expects('getRun')
- .withExactArgs({ actId, runId: run.id, waitForFinish: 999999, token: undefined })
+ .withExactArgs({ actId, runId: run.id, waitForFinish: 999999 })
.once()
.returns(Promise.resolve(runningRun));
actsMock.expects('getRun')
- .withExactArgs({ actId, runId: run.id, waitForFinish: 999999, token: undefined })
+ .withExactArgs({ actId, runId: run.id, waitForFinish: 999999 })
.once()
.returns(Promise.resolve(finishedRun));
@@ -380,16 +380,16 @@ describe('Apify.call()', () => {
const actsMock = sinon.mock(Apify.client.acts);
actsMock.expects('runAct')
- .withExactArgs({ actId, contentType: 'application/json; charset=utf-8', body: JSON.stringify(input, null, 2), token: undefined })
+ .withExactArgs({ actId, contentType: 'application/json; charset=utf-8', body: JSON.stringify(input, null, 2) })
.once()
.returns(Promise.resolve(runningRun));
actsMock.expects('getRun')
- .withExactArgs({ actId, runId: run.id, waitForFinish: 999999, token: undefined })
+ .withExactArgs({ actId, runId: run.id, waitForFinish: 999999 })
.once()
.returns(Promise.resolve(runningRun));
actsMock.expects('getRun')
- .withExactArgs({ actId, runId: run.id, waitForFinish: 999999, token: undefined })
+ .withExactArgs({ actId, runId: run.id, waitForFinish: 999999 })
.once()
.returns(Promise.resolve(finishedRun));
@@ -815,13 +815,13 @@ describe('Apify.callTask()', () => {
const tasksMock = sinon.mock(Apify.client.tasks);
tasksMock.expects('runTask')
- .withExactArgs({ token: undefined, taskId })
+ .withExactArgs({ taskId })
.once()
.returns(Promise.resolve(readyRun));
const actsMock = sinon.mock(Apify.client.acts);
actsMock.expects('getRun')
- .withExactArgs({ token: undefined, actId, runId: run.id, waitForFinish: 999999 })
+ .withExactArgs({ actId, runId: run.id, waitForFinish: 999999 })
.once()
.returns(Promise.resolve(failedRun));