2016-04-19 47 views
0

DelAPでRestAPIを使用してGoogleドライブにファイルをアップロードしようとしています。ドライブにファイルをアップロードすることはできません。 Googleドライブへの認証に成功しましたが、ファイルのアップロード中にエラーが発生しました。 以下は、私が得ているエラーメッセージです。ここでDelphiでGoogleドライブにファイルをアップロード中にエラーが発生しました

enter image description here

私のコードです:

var 
    LURL: string; 
    wv: Tfrm_OAuthWebForm; 
    LToken: string; 
    parents: TJSONArray; 
    Folder: TJSONObject; 
    upload_stream:TFileStream; 
local_filename : string; 
ttt: TJSONObject; 
begin 
    edt_GoogleTasks_AuthCode.Text := ''; 
    edt_GoogleTasks_AccessToken.Text := ''; 
    edt_GoogleTasks_RefreshToken.Text := ''; 

    /// step #1: get the auth-code 
    LURL := 'https://accounts.google.com/o/oauth2/auth'; 
    LURL := LURL + '?response_type=' + URIEncode('code'); 
    LURL := LURL + '&client_id=' + URIEncode(edt_GoogleTasks_ClientID.Text); 
    LURL := LURL + '&redirect_uri=' + URIEncode('urn:ietf:wg:oauth:2.0:oob'); 
    LURL := LURL + '&scope=' + URIEncode('https://www.googleapis.com/auth/tasks'); 
    // optional 
    // LURL := LURL + '&login_hint=' + URIEncode('[email protected]'); 

    wv := Tfrm_OAuthWebForm.Create(self); 
    try 
    wv.OnTitleChanged := self.OAuth2_GoogleTasks_BrowserTitleChanged; 
    wv.ShowModalWithURL(LURL); 
    finally 
    wv.Release; 
    end; 

    /// step #2: get the access-token 

    ResetRESTComponentsToDefaults; 

    RESTClient.BaseURL := 'https://accounts.google.com/'; 

    RESTRequest.Method := TRESTRequestMethod.rmPOST; 
    RESTRequest.Resource := 'o/oauth2/token'; 
    RESTRequest.Params.AddItem('code', edt_GoogleTasks_AuthCode.Text, TRESTRequestParameterKind.pkGETorPOST); 
    RESTRequest.Params.AddItem('client_id', edt_GoogleTasks_ClientID.Text, TRESTRequestParameterKind.pkGETorPOST); 
    RESTRequest.Params.AddItem('client_secret', edt_GoogleTasks_ClientSecret.Text, TRESTRequestParameterKind.pkGETorPOST); 
    RESTRequest.Params.AddItem('redirect_uri', 'urn:ietf:wg:oauth:2.0:oob', TRESTRequestParameterKind.pkGETorPOST); 
    RESTRequest.Params.AddItem('grant_type', 'authorization_code', TRESTRequestParameterKind.pkGETorPOST); 

    RESTRequest.Execute; 

    if RESTRequest.Response.GetSimpleValue('access_token', LToken) then 
    // edt_GoogleTasks_AccessToken.Text := LToken; 
    OAuth2_GoogleTasks.AccessToken := LToken; 
    if RESTRequest.Response.GetSimpleValue('refresh_token', LToken) then 
    // edt_GoogleTasks_RefreshToken.Text := LToken; 
    OAuth2_GoogleTasks.RefreshToken := LToken; 

    {$IF DEFINED(MsWindows)} 
    local_filename := ExtractFilePath(ParamStr(0)) +'Sanjeev.txt'; 
{$ENDIF} 
RESTResponseDataSetAdapter.AutoUpdate := false; 
RESTRequest.Params.Clear; 
           RESTRequest.ClearBody; 
//RESTRequest.Method:=rmpost; 
//try 
RESTClient.BaseURL :=  'https://www.googleapis.com/upload/drive/v2/files?'; 



     upload_stream := TFileStream.Create(local_filename,fmOpenReadWrite); 
    upload_stream.Position := 0; 
RESTRequest.AddBody(upload_stream,  TRESTContentType.ctAPPLICATION_OCTET_STREAM); 

try 
    RESTRequest.Execute;//Exception line 
except 
on e: Exception do 
begin 
    ShowMessage(e.Message); 
    end; 
     end; 
    upload_stream.Free; 

は編集:

最後に、私がGoogleドライブにファイルをアップロードすることができました。以前私が設定したapiのURLをLURL := LURL + '&scope=' + URIEncode('https://www.googleapis.com/auth/tasks')に変更しました。私はそれをLURL := LURL + '&scope=' + URIEncode('https://www.googleapis.com/auth/drive')に変更し、ファイルが正しくアップロードされています。

すべてがうまく見えました。しかし、下の画像のように無題のファイル名でファイルがアップロードされていることに気付きました。

enter image description here

いくつかのいずれかでは、Googleドライブに同じ名前のファイルをアップロード/ファイル名を与える方法を提案できます。

+0

デバッグを済ませましたか?あなたが送ったリクエストの内容を見ましたか? –

+0

@DavidHeffernan、私はデバッグして、私は行を追加することを逃したことがわかりました。私は追加しましたが、今は他の例外も見ています。私は同じ質問を編集しました。このエラーの原因は何ですか? –

+0

まだ、これをデバッグする必要があります。どのエラーコードがこのエラーを生成しますか?スタックトレースとは何ですか? –

答えて

1

最後にGoogleドライブにファイルをアップロードできました。以前私が設定したapiのURLをLURL := LURL + '&scope=' + URIEncode('https://www.googleapis.com/auth/tasks')に変更しました。私はそれをLURL := LURL + '&scope=' + URIEncode('https://www.googleapis.com/auth/drive')に変更し、ファイルが正しくアップロードされています。

関連する問題