2015-01-19 3 views
0

내 애플리케이션에서 Google 드라이브에 액세스해야합니다. 기능이 필요합니다. 특정 사용자가 처음으로 애플리케이션을 인증 할 때 필요합니다. Google에서 저장할 수있는 API의 정보가 필요합니다. 그리고 나서 Google 드라이브에 액세스하려고 할 때마다 서명하지 않았습니다. 사용자의 저장된 정보를 사용하여 드라이브 액세스를 위해 사용자를 자동으로 인증합니다. 오프라인 액세스로 많은 예제를 보았지만 내 목적을 해결할 수 없었습니다. 아래 사이트에서 Google 드라이브 액세스와 동일한 기능입니다. https://www.multcloud.com/Google 드라이브 오프라인 액세스 .net

누구나 위의 요구 사항을 충족시킬 수있는 방법이나 예를 들어주세요.

+0

API 문서는 무엇을 말하고 있습니까? 너 뭐 해봤 니? –

답변

0

Googles Client lib가 모든 것을 처리합니다. nuget.org/packages/Google.Apis.Drive.v2 기본적으로 FileDatastore를 사용합니다. Idatastore를 구현하고 새로 고침 토큰을 데이터베이스에 저장하는 것이 좋습니다.

Google 드라이브에 로그인하는 간단한 예입니다. Google.apis.drive.v2

String CLIENT_ID = "{...}.apps.googleusercontent.com"; 
      String CLIENT_SECRET = "GeE-cD7PtraV0LqyoxqPnOpv"; 

      string[] scopes = new string[] { DriveService.Scope.Drive, 
              DriveService.Scope.DriveFile}; 
      // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData% 
      UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = CLIENT_ID, ClientSecret = CLIENT_SECRET } 
                        , scopes 
                        , Environment.UserName 
                        , CancellationToken.None 
                        , new FileDataStore("Daimto.GoogleDrive.Auth.Store")).Result; 

      DriveService service = new DriveService(new BaseClientService.Initializer() 
      { 
       HttpClientInitializer = credential, 
       ApplicationName = "Drive API Sample", 
      }); 

Filedatastore 서버의 %의 AppData % 디렉터리에 모든게 저장됩니다 사용. 이것은 웹 애플리케이션에 이상적이지 않습니다. DatabaseDataStore.cs을보고 자신의 용도로 변경하는 것이 좋습니다.

코드가 발견 된 구글 드라이브 API C#을 튜토리얼 시리즈와 함께 간다 샘플 프로젝트에서 찢어 here

0

나는 내 응용 프로그램과 함께 Google 드라이브 연결과 같은 문제가 있었지만 지금은 하나 개의 솔루션을 발견하는 절대적으로 작업 벌금 나를. Googles Client lib가 너를 위해이 모든 것을 처리합니다. 누녜 패키지 Google.GData.Client 및 Google.GData.Documents를 다운로드하십시오. 당신이 다음 코드를 가지고 페이지 (의 Oauth 리디렉션 페이지) 3)에 돌아왔다 내 ​​코드

parameters = new OAuth2Parameters() 
       { 
        ClientId = "CLIENT_ID", 
        ClientSecret = "CLIENT_SECRET", 
        RedirectUri = currentURL,//"http://localhost:6600/Home.html", 
        Scope = "https://docs.google.com/feeds/ ", 
        State = "documents", 
        AccessType = "offline", // offline means it creats a refreshtoken 
        TokenExpiry = DateTime.Now.AddYears(1) 
       }; 
       string url = Google.GData.Client.OAuthUtil.CreateOAuth2AuthorizationUrl(parameters); 

1) 여기에 사용자가 그래서 여기 2 페이지를 리디렉션 자신의 계정으로 로그인해야합니다) 다음

URL에서 상태 (QueryString을) 4) 페이지로드 이벤트 (서버로 전송) 5) 당신에게 여기) 코드를 Page_Load 이벤트 (첫 번째 코드 변수에 쿼리 문자열)

OAuth2Parameters parameters = new OAuth2Parameters() 
      { 
       ClientId = "274488228041-1aaq8a069h3c7lsjstsl394725tumdlo.apps.googleusercontent.com", 
       ClientSecret = "Ew1EMwe4EB8oLHvKFfDZxQhp", 
       RedirectUri = currentURL,//"http://localhost:6600/Home.html" 
       Scope = "https://docs.google.com/feeds/ ", 
       State = "documents", 
       AccessType = "offline", // offline means it creats a refreshtoken 
       TokenExpiry = DateTime.Now.AddYears(1) 
      }; 
      parameters.AccessCode = code; 

      Google.GData.Client.OAuthUtil.GetAccessToken(parameters); 

1을 얻을 다음 쓰기 accesstoken 얻을

GOAuth2RequestFactory requestFactory = new GOAuth2RequestFactory(null, "Infactum", parameters); 
      DocumentsService service = new DocumentsService("Infactum"); 
      service.RequestFactory = requestFactory; 
      //requestFactory.CustomHeaders["Authorization"] = "Bearer " + parameters.AccessToken; 
      DocumentsListQuery query = new DocumentsListQuery(); 
      query.NumberToRetrieve = 2000; 

      // Make a request to the API and get all documents. 
      DocumentsFeed feed = service.Query(query); 

여기 Google 드라이브 문서에 액세스하는 매개 변수) 2 requesttoken 미래의 목적 (오프라인 액세스) 3 데이터베이스에 저장) 전달합니다, 당신은 공급 대상 2000 모든 유형의 파일을 얻을 것이다 당신 feed.entries를 사용하여 파일에 액세스 할 수 있습니다 ... 희망 하시길 바랍니다

관련 문제