2014-02-28 2 views
3

OAuth를 사용하여 Google 애널리틱스 API를 사용하고 싶습니다. 내가 승인 요청 모든을 수락하지 않도록Google 애널리틱스 OAuth with AccessType = 오프라인 C#

var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
    new ClientSecrets { ClientId = "...", ClientSecret = "..." }, 
    new[] {Google.Apis.Analytics.v3.AnalyticsService.Scope.AnalyticsReadonly}, 
    "user", 
    CancellationToken.None, 
    new FileDataStore("Analytics.Auth.Store")).Result; 

var service = new Google.Apis.Analytics.v3.AnalyticsService(
    new BaseClientService.Initializer 
    { 
     HttpClientInitializer = credential, 
     ApplicationName = "...", 
    }); 

내가 refresh_token도 이것을 사용할 수 : 다음 코드는 인증에 사용 http://code.google.com/p/google-api-dotnet-client/

:

나는이 라이브러리를 사용하고 있습니다 며칠? 이 질문의 답처럼

뭔가 : 당신이 GoogleAuthorizationCodeRequestUrl를 재정의해야합니다,하지만 난 방법 AuthorizationBroker 이것을 사용하는 아무 생각이 : Service Account Google Analytics OAuth AccessType = Offline C#

답변

12

나는 하나의 방법을 알고있다. GoogleWebAuthorizationBroker.cs

내가 PARAMS을 전달하거나 AuthorizationCodeFlow

+0

우리는 지금 설정하기 쉬웠다 인증서 서비스 계정을 사용에 다음 사용자 정의 FlowMetadata

public class AppFlowMetadata : FlowMetadata { private static readonly IAuthorizationCodeFlow flow = new CustomAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer { ClientSecrets = new ClientSecrets { ClientId = "...", ClientSecret = "..." }, Scopes = new String[] { AnalyticsService.Scope.AnalyticsReadonly }, DataStore = new EFDataStore(), }); public override IAuthorizationCodeFlow Flow { get { return flow; } } public override String GetUserId(Controller controller) { // In this sample we use the session to store the user identifiers. // That's not the best practice, because you should have a logic to identify // a user. You might want to use "OpenID Connect". // You can read more about the protocol in the following link: // https://developers.google.com/accounts/docs/OAuth2Login. return String.Format("user-{0}", WebSecurity.GetUserId(controller.User.Identity.Name)); } } 

및을, 그래서 나는이 방법을 시도하지 않았다. 나는 대답으로 표시하고있다. – Dennis

+0

Dennis, 언급 한 '인증서가있는 서비스 계정'을 설정하는 방법에 대한 지침이 있습니까? 나는별로 운이없는 노력하고있다. – Jarvis

+0

내 하루를 저장했습니다. 큰 도움. –

6

은 분명히 내가 말씀 드릴 수 없습니다 대체 할 수있는 방법을 보지 못했지만, 연장 : 그들은 브로커 내부 흐름을 만들처럼

internal class ForceOfflineGoogleAuthorizationCodeFlow : GoogleAuthorizationCodeFlow 
{ 
    public ForceOfflineGoogleAuthorizationCodeFlow(AuthorizationCodeFlow.Initializer initializer) : base(initializer) { } 

    public override AuthorizationCodeRequestUrl CreateAuthorizationCodeRequest(string redirectUri) 
    { 
     return new GoogleAuthorizationCodeRequestUrl(new Uri(AuthorizationServerUrl)) 
       { 
        ClientId = ClientSecrets.ClientId, 
        Scope = string.Join(" ", Scopes), 
        RedirectUri = redirectUri, 
        AccessType = "offline", 
        ApprovalPrompt = "force" 
       }; 
    } 
}; 

이 보이는 Darida의 대답에 :

public class CustomAuthorizationCodeFlow : GoogleAuthorizationCodeFlow 
{ 
    public CustomAuthorizationCodeFlow(GoogleAuthorizationCodeFlow.Initializer initializer) : base(initializer) { } 

    public override AuthorizationCodeRequestUrl CreateAuthorizationCodeRequest(String redirectUri) 
    { 
     return new GoogleAuthorizationCodeRequestUrl(new Uri(AuthorizationServerUrl)) 
     { 
      ClientId = ClientSecrets.ClientId, 
      Scope = string.Join(" ", Scopes), 
      RedirectUri = redirectUri, 
      AccessType = "online", 
      ApprovalPrompt = "auto" 
     }; 
    } 
} 
사용자 정의 CodeFlow 확인 0

그런 다음 컨트롤러

public ActionResult Sample() 
{ 
    var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).AuthorizeAsync(cancellationToken); 

    if (result.Credential != null) 
    { 
     var service = new AnalyticsService(new BaseClientService.Initializer() 
     { 
      HttpClientInitializer = result.Credential, 
      ApplicationName = APPLICATION_NAME 
     }); 
    } 
} 
+0

MVC없이 어떻게 할 수 있습니까? 모든 예제에서 웹 API 대신 MVC를 사용하는 것처럼 보입니다. –