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)}
    }
    });
    
     }

  • So this line below, to be exact, seems to be the culprit. There is nothing in the gallery settings that I know of says that I should be getting "Access to upload files is denied" when I check it during runtime while hovering fileInfo. Does it work for you? It simply isn't uploading to my dev environment. Is there a setting that I am missing? I have uploaded a test file manually to that gallery so I know that it exists. To where does the file upload with this line of code? Maybe that directory needs additional permissions on the server level or no?

    UploadedFileInfo fileInfo = host.UploadFile(file2);

  • The error you are receiving is returned when effective user making the file upload request is unauthenticated. Unauthenticated users cannot upload files.

  • I am not so sure about that and this is because I authenticate with user "admin" and the additional OAUTH credentials. I am able to use the same credentials to access a response using this code below:

    var response = host.GetToDynamic(2, "galleries.json", false); 

  • The REST SDK appears to be incompatible with v11 for file uploading. It is using an older endpoint for uploading files which was being transitioned away from in favor of the file upload REST API Endpoint (the old endpoint no longer exists in v12). In v11, API authentication is not honored on the old endpoint which causes every request to upload a file through the REST SDK to be handled as the unauthenticated/anonymous user which will always fail.

    The work-around is to upload the file against the upload endpoint ( https://community.telligent.com/community/11/w/api-documentation/64554/upload-cfs-rest-endpoint ) which requires directly writing a form-data encoded request with a single file (or portion of a file when chunking).

  • Thanks  I will look into this further and let you know if I get something working.

Reply Children
No Data