Unable to retrieve file content from google drive API

前端 未结 2 1427
南方客
南方客 2020-12-10 05:24

I seem to be unable to retrieve the content of a file via the google drive SDK. To reproduce the problem I use the API explorer to get the meta data for a small text file:

相关标签:
2条回答
  • 2020-12-10 05:33

    OK I finally worked it out. It seems for the GET request on the downloadURL you need to send the access token in the headers i.e send a "Authorization: Bearer {your access token}" header. DO NOT send the access token as part of the query string. Every other API call I have used in the Drive SDK is fine with the access token as part of the query string - except this one.

    0 讨论(0)
  • 2020-12-10 05:34

    Try to use below code for request

    var service1=SetCredential();
    var AccessToken=((Google.Apis.Auth.OAuth2.UserCredential)service1.HttpClientInitializer).Token.AccessToken;
    String link = "https://www.googleapis.com/drive/v2/files/" + fileId ;
    
    HttpWebRequest request = WebRequest.Create(link) as HttpWebRequest;
    request.Method = "GET";
    request.Headers.Add("Authorization", "Bearer " + AccessToken);
    WebResponse response = request.GetResponse();
    
    0 讨论(0)
提交回复
热议问题