Skip to content

LargeFileUploadTask documentation improvements for Client initialization #449

@kairiruutel

Description

@kairiruutel

Would be useful to have an example in documentation about how to implement the LargeFileUploadTask with correct Client initialisation in regard of authorisation. Currently it is not clear that a new Client has to be initialised with authtoken from the UploadSession response to prevent 401 error.

Adding code example how I got it working:

const Url = require('url').URL
const { Client, LargeFileUploadTask, FileUpload } = require('@microsoft/microsoft-graph-client');

const graphClient = Client.init({
    authProvider: callback => callback(null, authHeader.token),
});

const { id } = await createMessage(graphClient); // creates new message through /me/messages

const uploadSession = await LargeFileUploadTask.createUploadSession(
    graphClient,
    `/me/messages/${messageId}/attachments/createUploadSession`,
    {
	AttachmentItem: {
		attachmentType: 'file',
		name: file.name,
		size: file.size,
	},
    }
);
const uploadUrl = new Url(uploadSession.url);
const uploadUrlAuthToken = uploadUrl.searchParams.get('authtoken');

const uploadClient = Client.init({
	authProvider: (done) => done(null, uploadUrlAuthToken)
});

const uploadTask = new LargeFileUploadTask(
	uploadClient,
	new FileUpload(
		file.content,
		file.name,
		file.size
	),
	uploadSession,
	{
		rangeSize: 5 * 1024 * 1024
	}
);

await uploadTask.upload();

AB#9349

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions