簡體   English   中英

在 C# 中從谷歌驅動器下載視頻的流媒體鏈接

[英]Download streaming link of video from google drive in C#

由於我的英語不好,我將在示例中為您提供詳細信息,以便你們理解我的問題並幫助我:(

例如,我想從這里下載視頻:

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

使用 Chrome DevTool,網絡面板,我抓住了請求在此處輸入圖片說明

Chrom DevTool 中顯示的“請求 URL”是我想要的流媒體鏈接

我的問題是:有沒有辦法在不在瀏覽器中打開的情況下獲取這個“請求 URL”? 我嘗試使用 C# 和 google drive rest API,但經過一段時間的研究,我不知道我該怎么辦:(。我所做的只是按照本指南列出驅動器中存儲的所有文件。但我嘗試得到webcontentlink 或 webviewlink 響應 null :(

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

我在 C# 中問你,因為我想在這方面做得更好,但是如果你在 Java 或其他方面有很好的解決方案,請分享它並向我解釋:(

非常感謝

這是您需要的代碼,但首先您必須按照https://developers.google.com/drive/v3/web/quickstart/dotnet 中的說明創建 API 密鑰並將 client_secret.json 文件復制到您的 c# 解決方案

對於這個例子,我使用了一個 Winform 項目應用程序:我通過谷歌驅動器 API 檢索 WebViewLink,然后我在 winform 應用程序中播放視頻

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


}

}

結果截圖: 結果截圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM