Unable to Upload A File to Gallery with C# REST API with Error Message

I am using the following code to attempt to upload a file programmatically using C# REST API to an existing Gallery. I have tried to Update the Gallery and also Post to the gallery and I keep getting the message: "Access to Upload Files Denied" error from fileInfo.Message. Please let me know what causes this message and how to get the code to work properly.

var driveItemPath = Path.Combine(@"C://CompanyName//", fileName);

using (var driveItemFile = System.IO.File.Create(driveItemPath))
{
stream.Seek(0, SeekOrigin.Begin);
stream.CopyTo(driveItemFile);
}
//Set credentials
const string USER_NAME = "admin";
const string OAUTH_CLIENT_ID = "";
const string OAUTH_SECRET = "";


using (var fileStream = new FileStream(driveItemPath, FileMode.Open, FileAccess.Read))
{
fileStream.Position = 0;
Guid uploadContextId = Guid.NewGuid();
UploadedFile file2 = new UploadedFile(uploadContextId, fileName, MimeMapping.GetMimeMapping(fileName), fileStream);

var host = new ClientCredentialsRestHost(USER_NAME, "">http://company.com", OAUTH_CLIENT_ID, OAUTH_SECRET);
UploadedFileInfo fileInfo = host.UploadFile(file2);

try
{
host.Impersonate("admin", (h) =>
{
// Post file to Existing Gallery
var pathParameters = new NameValueCollection();
pathParameters.Add("mediagalleryid", "6");
dynamic mediaResponse = host.PostToDynamic(2, "media/{mediagalleryid}/files.json", true,
new RestPostOptions()
{
PathParameters = pathParameters,
PostParameters = new System.Collections.Specialized.NameValueCollection {
{ "Name", fileName },
{ "FileName", fileName.Trim()},
{ "FileUploadContext", fileInfo.UploadContext.ToString()},
{ "ContentType", MimeMapping.GetMimeMapping(fileName)}
}
});

Parents
  • You are trying ti impersonate the admin with admin , please try removing the impersonation  as below

    using (var fileStream = new FileStream(driveItemPath, FileMode.Open, FileAccess.Read))
    {
    fileStream.Position = 0;
    Guid uploadContextId = Guid.NewGuid();
    UploadedFile file2 = new UploadedFile(uploadContextId, fileName, MimeMapping.GetMimeMapping(fileName), fileStream);
    
    var host = new ClientCredentialsRestHost(USER_NAME, "">http://company.com", OAUTH_CLIENT_ID, OAUTH_SECRET);
    UploadedFileInfo fileInfo = host.UploadFile(file2);
    
    try
    {
    
    // Post file to Existing Gallery
    var pathParameters = new NameValueCollection();
    pathParameters.Add("mediagalleryid", "6");
    dynamic mediaResponse = host.PostToDynamic(2, "media/{mediagalleryid}/files.json", true,
    new RestPostOptions()
    {
    PathParameters = pathParameters,
    PostParameters = new System.Collections.Specialized.NameValueCollection {
    { "Name", fileName },
    { "FileName", fileName.Trim()},
    { "FileUploadContext", fileInfo.UploadContext.ToString()},
    { "ContentType", MimeMapping.GetMimeMapping(fileName)}
    }
    });
    
     }

  • I have removed the impersonation code and I still get that message. The exact message is: "Access to upload files is denied". I checked within our dev instance and permissions to upload file(s) looks to be okay. Is there any other setting that might be preventing the upload and is giving that message? 

Reply Children
No Data