从C#谷歌驱动器下载视频的流媒体链接

时间:2018-02-05 17:50:39

标签: c# google-api google-drive-api

由于我的英语不好,我会在例子中给你详细信息,这样你们就能理解我的问题并帮助我:(

例如,我想从这个下载视频:

  

https://drive.google.com/file/d/0B6zj9fZgMGr7dXl3Z3VxSGRadU0/view

使用Chrome DevTool,网络面板,我抓住了请求 enter image description here

"请求网址"如Chrom DevTool中所示,是我想要的流媒体链接

我的问题是:有没有办法获得这个"请求网址"没有在浏览器中打开?我尝试使用C#和谷歌驱动器休息API但经过一段时间的研究后,我不知道该怎么办:(。 我所做的就是按照本指南列出我的驱动器中的所有文件存储。但我试图让webcontentlink或webviewlink响应为null :(

  

https://developers.google.com/drive/v3/web/quickstart/dotnet

我在C#中问你,因为我想在这方面做得更好,但是如果你有很好的Java解决方案或者什么,请分享并向我解释:(

非常感谢

1 个答案:

答案 0 :(得分:0)

以下是您需要的代码,但首先您必须按照https://developers.google.com/drive/v3/web/quickstart/dotnet中的说明创建API密钥并将client_secret.json文件复制到您的c#解决方案

对于这个例子,我使用了一个Winform项目应用程序:我通过谷歌驱动API检索WebViewLink,然后我在winform App中播放视频

using System;
using System.Windows.Forms;
using System.IO;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System.Threading;

public partial class Form1 : Form
{
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/drive-dotnet-quickstart.json
static string[] Scopes = { DriveService.Scope.DriveReadonly };
static string ApplicationName = "Drive API .NET Quickstart";


public Form1()
{
    InitializeComponent();
    UserCredential credential;

    using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
    {
        string credPath = System.Environment.GetFolderPath(
            System.Environment.SpecialFolder.Personal);
        credPath = Path.Combine(credPath, ".credentials/drive-dotnet-quickstart.json");

        credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            GoogleClientSecrets.Load(stream).Secrets,
            Scopes,
            "user",
            CancellationToken.None,
            new FileDataStore(credPath, true)).Result;
        Console.WriteLine("Credential file saved to: " + credPath);
    }

    // Create Drive API service.
    var service = new DriveService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
        ApplicationName = ApplicationName,
    });
 //here is your request file id taken from https://drive.google.com/file/d/0B6zj9fZgMGr7dXl3Z3VxSGRadU0/view
    FilesResource.GetRequest getRequest =  service.Files.Get("0B6zj9fZgMGr7dXl3Z3VxSGRadU0");
    getRequest.Fields = "webViewLink";
    Google.Apis.Drive.v3.Data.File file = getRequest.Execute();
   //here is the video link you wanted
    string sourceURL = file.WebViewLink;

    //play the video in Winform
    webBrowser1.Url = new Uri(sourceURL);


}

}

结果截图: results screenshot