Search code examples
c#restgoogle-drive-apigoogle-oauth

Google Drive insert file permission


I can't insert permission to a file with this code:

string URI = String.Format("https://www.googleapis.com/drive/v2/files/{0}/permissions&access_token={1}", fileId, "token");

var request = (HttpWebRequest)HttpWebRequest.Create(URI);
request.Method = "POST";
request.ContentType = "application/json"; 

string json = "{\"role\": \"reader\",\"type\": \"anyone\"}";

byte[] byteData = new System.Text.ASCIIEncoding().GetBytes(json);

request.ContentLength = byteData.Length;
using (var dataStream = request.GetRequestStream())
{
    dataStream.Write(byteData, 0, byteData.Length);
}

var response = (HttpWebResponse)request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
    json = reader.ReadToEnd();
}

I al getting a 404 error. What's the problem?


Solution

  • string URI = String.Format("https://www.googleapis.com/drive/v2/files/{0}/permissions&access_token={1}", fileId, "token");
    

    Access token is not a string "token" it must be a valid access token for the user who owns the file.

    Update:

    permissions?access_token={1}", 
    

    You should be using ? and not & to add a parameter to the url. Not even sure you can do it like that with a HTTP Post.

    Added info:

    1. If this is not simply a typo on your part you may want to read up on Authorization a little
    2. I also recommend checking out the Google client library instead of writing this yourself. Google.Apis.Drive.v2 Client Library.
    3. There is a newer version of the Google Drive API you might also be interested in looking at rather then writing new code for an older version of the API. Google Drive API v3.