Accessing file using fileget url - node.js

I am trying to create file handler add in that uses provider hosted application to open custom file.
I have hosted app in heroku and until now i get request body in my application as follows when i open file from sharepoint.
{
resourceid: "https://{tenant}.sharepoint.com",
culturename: "en-US",
fileget: "https://{tenant}.sharepoint.com/_vti_bin/wopi.ashx/files/{fileid}/contents?access_token={token}",
fileput: "https://{tenant}.sharepoint.com/_vti_bin/wopi.ashx/files/{fileid}/contents?access_token={token}",
fileid: "{fileid}",
client: "SharePoint"
}
But when i request to get file content using fileget url. It throws 500 error with message
The URL is invalid for the current user or application.
What could be the way to access file content.

The URL seem to be correct.
What you can do is to see the file's URL properties by requesting CheckFileInfo.
Example:
GET https://{tenant}.sharepoint.com/_vti_bin/wopi.ashx/files/{fileid}?access_token={token}
And have a look at DownloadUrl and FileUrl. If any of the properties is set, use them to download the file.

Related

Can I use HTML5 download attribute in anchor tag using an API (instead of direct link)?

I have a web page (served from, say, www.example.com domain after user logged in) that has following html element.
<a href="../api/values/import" download>Download Import File</a>
The import file is currently saved in a azure storage as a blob. Assume that the blob (i.e. import file) requires temporary SAS token (created for logged in user) to be accessed:
https://foo.blob.core.windows.net/myfiles/import.txt?(sasTokenInfo)
All of this can be simplified if the anchor tag can be used as shown below:
<a href="https://foo.blob.core.windows.net/myfiles/import.txt?(sasTokenInfo)" download>Download Import File</a>
However this runs the risk of expired sasTokenInfo before user clicks on it. The user may linger on this page (which has other info on it) sufficiently long enough for the sasToken to expire. The simplest thing to do here is to create sasToken for longer period. But I don't want to do that.
I am trying to find out if better solution is to use the API as shown at the beginning. When the user clicks on the above anchor link (i.e "../api/values/import"), the idea is to have this API create the blob sasToken for this user and send the link (that contains this sasToken) to the above blob storage import file. The idea is not to read this file in the above API but simply send the link to it so that browser can download it directly without involving the domain www.foo.com. To facilitate this, I thought if I can have the following header information, I would be able to force the browser to download this file directly from azure blob storage on the browser's machine:
Content-Disposition: attachment; filename="https://foo.blob.core.windows.net/myfiles/import.txt"
Apparently, the value of "filename" (from Content-Disposition header value) should not contain path info (See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition). This means I am not able to set the direct blob storage related link in this header value.
Questions:
Is the API route a viable option?
If yes, then how can I send a link to a file in the azure storage through my API to the browser so that browser can automatically download the file?
What do I need to do in my asp net core web API for (2)?
What do I need to do on the client-side html pages for (2)?
If yes, then how can I send a link to a file in the azure storage through my API to the browser so that browser can automatically download the file?
I suggest you could use ajax to let the web api to generate the blob url and then create the herf html to download it inside the succss method.
More details, you could refer to below codes:
Client ajax:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$.ajax({
url: 'https://localhost:7204/api/values/Download',
method: 'GET',
success: function (data) {
var link = document.createElement('a');
link.href = data;
link.download = 'myFile.txt'; // Set the file name here
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
},
error: function () {
console.log('Failed to download file.');
}
});
</script>
Web API:
[HttpGet("Download")]
public string GetUrl() {
return "https://localhost:7204/111.txt";
}
Result:
It will download the file automatically.

Error 400 Invalid request with upload url but file is properly send to sharepoint

I try to send a file to sharepoint using the Microsoft Graph API resumable file upload :
First, I create my upload session POST: https://graph.microsoft.com/v1.0/me/drive/root:/{itemid}/createUploadSession and I get my upload url, no problem here.
I call my upload url PUT: https://xxx-my.sharepoint.com/personal/xxx_onmicrosoft_com/_api/v2.0/drive/items/xxxxxxxxxx/uploadSession?guid='xxxx'&overwrite=True&rename=False&dc=0&tempauth=xxx with some parameters: Content-Length and Content-Range
I get this error, code 400 : {"error":{"code":"invalidRequest","message":"Invalid request"}} but if I take a look to my sharepoint, the upload did create it! I tried to add or remove parameters, change PUT to POST but of course, it's not working (got DeferCommit was set to false for this upload session., which seems to be normal if I call the upload url with POST).
If I look to the error message, I just have Bad Request with no more informations. I don't know what I'm doing wrong or what I'm missing. If you have some insights, please do tell!!
I found what was missing! I need to add this header to my request: Accept with the value application/json. Now it works like a charm!

Azure copy data failed to read data from http server

I am new to ADF. I created an http linked service and it tested successfully (https://rebrickable.com/downloads/). On the http dataset properties page the same url address is in the Base URL. I then put just the file name that exists on that page (themes.csv.gz) in the Relative URL. When I click next to the File format settings page the Preview pane says:
Failed to read data from http server. Check the error from http
server:The remote server returned an error: (404) Not Found. The
remote server returned an error: (404) Not Found. . Activity ID:
0ca9f9c7-438d-461a-81c1-f81bacace4ca
What am I doing wrong please?
I also tried to create a new linked service and point it to my desktop files. I selected'File', left it at AutoresolveIntegrationruntime, put the path in Host. But don't know what do to with User/password. Please help
I hit the above mentioned end point in by browser : https://rebrickable.com/downloads/themes.csv.gz
Looks like the file is not existent at the mentioned location.
I did a quick scan of the page https://rebrickable.com/downloads
Encountered the below file :
Saw the file hyperlinked to this url (This is different from what you're attempting to in the ADF)
https://cdn.rebrickable.com/media/downloads/themes.csv.gz
I was able to download the file by using the above.
Would suggest you to try the above endpoint.
Update your url to https://cdn.rebrickable.com/media/downloads/ in https connection source

Google Site Verification Failure - HTML File Upload method

I have an Express/NodeJS app running on Google App Engine, for which I have the URL in the format of:
project-name.appspot.com, where project-name: My google project name
Now, for site verification, I am using HTML File Upload method.
I am serving the html verification file provided by google as follows:
res.sendFile(path.join(__dirname, path_to_html_file))
Now, when I enter the url: https://project-name.appspot.com/, I can view the file.
Also, the file has the same name as provided by Google.
However, when I click Verify in Webmaster, it still fails with error message "File not Found"
Can anyone point what I might be doing incorrectly ?
You need to serve this file from the /xxxxx.html url, not the root url.
In other words, it needs to be accessed from https://project-name.appspot.com/xxxxxxxx.html
Think about it... This file must stay up for as long as you want to be verified, so it doesn't make sense to be shown at the root url.
Also, make sure there are no redirects in serving this file.

Using OAuth dialog for facebook app do not allow to use canvas URL as redirect_uri

I am starting a Facebook app. Following the Getting Started tutorial in the Authorization section, it says I should use this URL to get permission from users:
https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_CANVAS_PAGE
I am replacing YOUR_CANVAS_PAGE with my canvas URL, the one I see on my app settings:
https%3A%2F%2Fapps.facebook.com%2F238620302882463%2F
But, then, if I navigate to that page, I get the following error:
An error occurred with Elecciones 2012. Please try again later.
API Error Code: 191
API Error Description: The specified URL is not owned by the application
Error Message: Invalid redirect_uri: Given URL is not allowed by the Application configuration.
If I replace YOUR_CANVAS_PAGE with:
http%3A%2F%2Fwww.example.com%2FElecciones2012
The permission dialog works fine. But then I get redirected to my website, not the app inside facebook.com
Any idea why is this happening??
I have seen other apps using a different permission dialogue:
http://www.facebook.com/connect/uiserver.php?app_id=11609831134&method=permissions.request&redirect_uri=http%3A%2F%2Fapps.facebook.com%2Fpetsociety%2F%3Fpf_ref%3Dsb%26ref%3Dts&response_type=none&display=page&perms=email%2Cpublish_actions&auth_referral=1
But it looks it is part of another set of APIs.
I got the same problem too. Looks like the problem is in "Canvas URL". You cannot use your app id in canvas URL like:
"https%3A%2F%2Fapps.facebook.com%2F238620302882463%2F"
Instead, the namespace should be used as your canvas URL. for example:
"https://apps.facebook.com/myapplication/"
You can set your app namespace in the application settings in facebook.

Resources