3

特定のフォルダーの下にあるすべてのアイテムを取得しようとしています。

私はこのドキュメントを使用していました:
https://developers.google.com/drive/v2/reference/files/list

そしてそれをもとに次のように書きました。

string url = string.Format("https://www.googleapis.com/drive/v2/files?'q'=\"'{0}' in parents\"", folderId);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = WebRequestMethods.Http.Get;
WebResponse response = request.GetResponse();

そして、私はエラーが発生します:

(401) 無許可

または、リクエストをコピーしてブラウザに貼り付けると、次のようになります。

{ "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login Required", "locationType": "header", "location": " Authorization" } ], "code": 401, "message": "Login Required" } }

この質問も参照として使用しました:
ドライブ SDK のフォルダーごとにファイルのリストを取得しています

編集:
認証を含むこのパラメーターがあります:

DriveService service  

以前は、すべてのファイルを取得するために、次のようにしていました。

FilesResource.ListRequest request = service.Files.List();   

しかし今、特定のアイテムを取ろうとしているとき、それをどのように組み合わせるかわかりませんservice

4

4 に答える 4

3

有効なアクセス トークンを使用してリクエストを認証する必要があります。アクセス トークンを取得する方法、または OAuth 2.0 サポートに付属する Java クライアント ライブラリを使用する方法については、OAuth 2.0 を参照してください [2]。

[1] https://developers.google.com/accounts/docs/OAuth2

[2] https://developers.google.com/drive/auth/web-server

于 2013-07-15T14:14:22.510 に答える
3

C# クイックスタートを見始めることができます。

クイック スタートでは、コンソールから承認してから、Google ドライブにファイルを挿入する方法を示します。

.Net ライブラリをプロジェクトにインポートした後、次のようなコードを記述してファイルを取得できます。

Private Sub GetFileList(ParentFolder As String)

    Authorize() 'Take care of authorization... you could use JS to ask the user and get the Auth Token

    'MSO - 20130423 - Search the Google Drive File with the specified foldername
    'Create the search request
    Dim oListReq As Google.Apis.Drive.v2.FilesResource.ListRequest
    Dim oFileList As FileList
    'mimeType = 'application/vnd.google-apps.folder'

    oListReq = oDriveService.Files.List()
    'Search for a specific file name
    oListReq.Q = "mimeType = 'application/vnd.google-apps.folder' and title = '" + ParentFolder + "' and trashed=false"
    oListReq.Fields = "items/id" 'MSO - 20130621 - only ID needed for next query
    oListReq.MaxResults = 10 'Max 10 files (too may I Expect only 1)

    'Get the results
    oFileList = oListReq.Fetch()

    'Only 1 result is expected
    If oFileList.Items.Count = 1 Then
        Dim oFile As File = oFileList.Items(0)
        FolderId = oFile.Id 'Get FolderId
    End If

    oListReq = oDriveService.Files.List()
    'Search for a specific file name in the folder
    oListReq.Q = "'" + FolderId + "' in parents and trashed=false "
    'oListReq.Fields = "items(id,alternateLink)" 'MSO - 20130621 - Optimize your query if you need only certain fields

    'Get the results
    oFileList = oListReq.Fetch()

    'TODO: oFileList now have the list of the files in the folder, but there could me more "pages"

End Sub

テストされていませんが、本番環境で実行するコードに大きく基づいているため、動作するはずです

于 2013-07-15T21:06:35.343 に答える
1

googleApi V3 を使用しています。これはサンプルコードです:

 string FolderId = "1lh-YnjfDFMuPVisoM-5p8rPeKkihtw9";
        // Define parameters of request.
        FilesResource.ListRequest listRequest = DriveService.Files.List();
        listRequest.PageSize = 10;
        listRequest.Q = "'" + FolderId + "' in parents and trashed=false";
        listRequest.Fields = "nextPageToken, files(*)";

        // List files.
        IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()
            .Files;

この助けを願っています。

于 2020-03-13T08:00:12.880 に答える
0

If you have a problem in retrieving files and folder from a specific folder then follow this link it will help you in detail Get all files and folder from specific folder in Google Drive

于 2014-04-03T11:18:38.993 に答える