2017-07-06 14 views
0

Googleドライブ(pdf、word、excel)にファイルをアップロードする必要があります.C#を使用してASP.NETでどのように行うことができますか?あなたは私にすべての通路を一歩一歩説明してくれますか?それが役立つことができれば、私は私の最後のテストコード置く:ファイルをGoogleドライブにアップロード

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using ASPSnippets.GoogleAPI; 
using System.Runtime.Serialization; 
using System.IO; 
using System.Web.Script.Serialization; 

... 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     GoogleConnect.ClientId = "my_client_id"; 
     GoogleConnect.ClientSecret = "my_client_secret"; 
     GoogleConnect.RedirectUri = Request.Url.AbsoluteUri.Split('?')[0]; 
     GoogleConnect.API = EnumAPI.Drive; 
     if (!string.IsNullOrEmpty(Request.QueryString["code"])) 
     { 
      string code = Request.QueryString["code"]; 
      string json = GoogleConnect.PostFile(code, (HttpPostedFile)Session["File"], Session["Description"].ToString()); 
      GoogleDriveFile file = (new JavaScriptSerializer()).Deserialize<GoogleDriveFile>(json); 
      tblFileDetails.Visible = true; 
      lblTitle.Text = file.Title; 
      lblId.Text = file.Id; 
      imgIcon.ImageUrl = file.IconLink; 
      lblCreatedDate.Text = file.CreatedDate.ToString(); 
      lnkDownload.NavigateUrl = file.WebContentLink; 
      if (!string.IsNullOrEmpty(file.ThumbnailLink)) 
      { 
       rowThumbnail.Visible = true; 
       imgThumbnail.ImageUrl = file.ThumbnailLink; 
      } 
     } 
     if (Request.QueryString["error"] == "access_denied") 
     { 
      ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Access denied.')", true); 
     } 
    } 

    protected void UploadFile(object sender, EventArgs e) 
    { 
     Session["File"] = FileUpload1.PostedFile; 
     Session["Description"] = txtDescription.Text; 
     GoogleConnect.Authorize("https://www.googleapis.com/auth/drive.file"); 
    } 
} 


public class GoogleDriveFile 
{ 
    public string Id { get; set; } 
    public string Title { get; set; } 
    public string OriginalFilename { get; set; } 
    public string ThumbnailLink { get; set; } 
    public string IconLink { get; set; } 
    public string WebContentLink { get; set; } 
    public DateTime CreatedDate { get; set; } 
    public DateTime ModifiedDate { get; set; } 
} 

をしかし、私はこのエラーがあります:

  1. That’s an error.

Error: redirect_uri_mismatch

The redirect URI in the request, http://localhost:61360/Prova.aspx , does not match the ones authorized for the OAuth client. Visit https://console.developers.google.com/apis/credentials/oauthclient/831126948299-2bopcsp3aee3harncqp5dpkq77sii3he.apps.googleusercontent.com?project=831126948299 to update the authorized redirect URIs.

要求の詳細:

scope=https://www.googleapis.com/auth/drive.file 
redirect_uri=http://localhost:61360/Prova.aspx 
response_type=code 
client_id=831126948299-2bopcsp3aee3harncqp5dpkq77sii3he.apps.googleusercontent.com 
approval_prompt=auto 
access_type=offline 

を私はリダイレクトページを挿入しましたが、それは私に同じエラーを与えた。

+1

OAuthサイトに登録すると、アプリが承認されると、そのユーザーにリダイレクトするウェブページが表示されます。エラーメッセージには、 '' http:// localhost:61360/Prova.aspx''はあなたが登録したものではありません。 Google Appsに登録したURLは何ですか? – Neil

+0

私はリダイレクトページを挿入しません...私はアプリケーションを作成し、私のプログラムでIDと秘密を使用しました。リダイレクトURLとして何を配置すればよいですか? –

+0

多分私は説明することができます: "console.developers.google.com"では、 "資格"、 "IDクライアントOAuth 2.0"で私は作成した名前フィールドだけを入れました。 「コンセンサス製品の画面」では、メールアドレスと製品名が –

答えて

0

Googleデベロッパーコンソールに追加するリダイレクトURIは、あなたが送信しているものと正確に一致する必要があります。http://localhost:61360を送信しているので、リダイレクトURIにhttp://localhost:61360を追加する必要があります。または可能であればhttp://localhost:61360/AuthCallback

ヒント:ランダムなポート番号を追加しないようにビジュアルスタジオを設定してください。

+0

できるだけ早く試してみる –

0

私はリダイレクトURIを追加しましたが、プログラムを再開しようとすると、別のURIを承認するようにエラーが再び表示されます。私もそれを追加しようとしましたが、別のURIでプログラムを再起動するたびにエラーが再発します。ランダムポートを無効にするにはどうしたらいいですか?

関連する問題