简体   繁体   English

在Windows Store App中将文件上传/下载到Google云端硬盘

[英]Upload/Downloading files to Google Drive in Windows Store App

I am able to sign into Google using the web authentication sample . 我可以使用网络身份验证示例登录Google。 Here's the code: 这是代码:

    private async void Launch_Click(object sender, RoutedEventArgs e) 
    {    
        if(GoogleClientID.Text == "") 
        { 
            rootPage.NotifyUser("Please enter an Client ID.", NotifyType.StatusMessage); 
        } 
        else if(GoogleCallbackUrl.Text == "") 
        { 
            rootPage.NotifyUser("Please enter an Callback URL.", NotifyType.StatusMessage); 
        } 

        try 
        { 
            String GoogleURL = "https://accounts.google.com/o/oauth2/auth?client_id=" + Uri.EscapeDataString(GoogleClientID.Text) + "&redirect_uri=" + Uri.EscapeDataString(GoogleCallbackUrl.Text) + "&response_type=code&scope=" + Uri.EscapeDataString("http://picasaweb.google.com/data"); 

            System.Uri StartUri = new Uri(GoogleURL); 
            // When using the desktop flow, the success code is displayed in the html title of this end uri 
            System.Uri EndUri = new Uri("https://accounts.google.com/o/oauth2/approval?"); 

            DebugPrint("Navigating to: " + GoogleURL); 

            WebAuthenticationResult WebAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync( 
                                                    WebAuthenticationOptions.UseTitle, 
                                                    StartUri, 
                                                    EndUri); 
            if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success) 
            { 
                OutputToken(WebAuthenticationResult.ResponseData.ToString()); 
            } 
            else if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.ErrorHttp) 
            { 
                OutputToken("HTTP Error returned by AuthenticateAsync() : " + WebAuthenticationResult.ResponseErrorDetail.ToString()); 
            } 
            else 
            { 
                OutputToken("Error returned by AuthenticateAsync() : " + WebAuthenticationResult.ResponseStatus.ToString()); 
            } 
        } 
        catch (Exception Error) 
        { 
            // 
            // Bad Parameter, SSL/TLS Errors and Network Unavailable errors are to be handled here. 
            // 
            DebugPrint(Error.ToString()); 
        } 
}       

After the app authenticates, I'm not sure how to go about using file operations. 应用验证后,我不确定如何使用文件操作。 Their is a .Net library that Google has made available but it doesn't seem to work for Windows store apps. 它们是Google提供的.Net库,但似乎不适用于Windows应用商店。

Here is the doc upload files over http: 这是通过http上传的文档:

Google Drive api: https://developers.google.com/drive/manage-uploads Google云端硬盘api: https : //developers.google.com/drive/manage-uploads

I tried using the HttpClient sample but wasn't sure how to use the authentication token that I got from the authentication sample. 我尝试使用HttpClient示例,但不确定如何使用从身份验证示例获得的身份验证令牌。

It looks like you'd be best served using the BackgroundUploader class in WinRT, and then putting your auth token into the Authorization header using the SetRequestHeader method. 看起来最好使用WinRT中的BackgroundUploader类,然后使用SetRequestHeader方法将您的auth令牌放入Authorization标头中,从而为您提供最好的服务。 The BackgroundUploader should handle the parts of your file (based on the Google Drive documentation and the MSDN docs) so with the headers and method below, I think that's all you need. BackgroundUploader应该处理文件的各个部分(基于Google Drive文档和MSDN文档),因此,使用下面的标题和方法,我认为这就是您所需要的。

POST /upload/drive/v2/files?uploadType=media HTTP/1.1
Host: www.googleapis.com
Content-Type: image/jpeg
Content-Length: {number_of_bytes_in_JPEG_file}
Authorization: {your_auth_token}

{JPEG data}

For smaller file types, you should be able to do something similar with System.Net.HttpClient - here is a post about setting headers on that type. 对于较小的文件类型,您应该可以使用System.Net.HttpClient进行类似的操作- 是有关在该类型上设置标头的文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM