2014-12-16 4 views
4

Google에 인증 요청에 login_hint를 추가하고 싶습니다. 브라우저가 열리기 전에 메일 주소가 추가됩니다 그래서이 매개 변수를 전달하지GoogleWebAuthorizationBroker에 "&[email protected]"을 추가하는 방법

FileDataStore fDS = new FileDataStore(Logger.Folder, true); 
GoogleClientSecrets clientSecrets = GoogleClientSecrets.Load(stream); 
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
           clientSecrets.Secrets, 
           scopes.ToArray(), 
           username, 
           CancellationToken.None, 
           fDS). 
           Result; 
var initializer = new Google.Apis.Services.BaseClientService.Initializer(); 
initializer.HttpClientInitializer = credential; 

: 다음 코드를 사용하고 있습니다?

답변

3

Zhaph의 힌트에 감사드립니다! 지금까지

내 솔루션 : (

using Google.Apis.Auth.OAuth2; 
using Google.Apis.Auth.OAuth2.Flows; 
using Google.Apis.Auth.OAuth2.Requests; 
using Google.Apis.Util.Store; 
using System; 
using System.Collections.Generic; 
using System.Threading; 
using System.Threading.Tasks; 

namespace MyOAuth2 
{ 
    //own implementation to append login_hint parameter to uri 

    public class MyOAuth2WebAuthorizationBroker : GoogleWebAuthorizationBroker 
    { 
     public new static async Task<UserCredential> AuthorizeAsync(ClientSecrets clientSecrets, 
      IEnumerable<string> scopes, string user, CancellationToken taskCancellationToken, 
      IDataStore dataStore = null) 
     { 
      var initializer = new GoogleAuthorizationCodeFlow.Initializer 
      { 
       ClientSecrets = clientSecrets, 
      }; 
      return await AuthorizeAsyncCore(initializer, scopes, user, taskCancellationToken, dataStore) 
       .ConfigureAwait(false); 
     } 

     private static async Task<UserCredential> AuthorizeAsyncCore(
      GoogleAuthorizationCodeFlow.Initializer initializer, IEnumerable<string> scopes, string user, 
      CancellationToken taskCancellationToken, IDataStore dataStore = null) 
     { 
      initializer.Scopes = scopes; 
      initializer.DataStore = dataStore ?? new FileDataStore(Folder); 
      var flow = new MyAuthorizationCodeFlow(initializer, user); 

      // Create an authorization code installed app instance and authorize the user. 
      return await new AuthorizationCodeInstalledApp(flow, new LocalServerCodeReceiver()).AuthorizeAsync 
       (user, taskCancellationToken).ConfigureAwait(false); 
     } 
    } 

    public class MyAuthorizationCodeFlow : GoogleAuthorizationCodeFlow 
    { 
     private readonly string userId; 

     /// <summary>Constructs a new Google authorization code flow.</summary> 
     public MyAuthorizationCodeFlow(Initializer initializer, string userId) 
      : base(initializer) 
     { 
      this.userId = userId; 
     } 

     public override AuthorizationCodeRequestUrl CreateAuthorizationCodeRequest(string redirectUri) 
     { 
      return new GoogleAuthorizationCodeRequestUrl(new Uri(AuthorizationServerUrl)) 
      { 
       ClientId = ClientSecrets.ClientId, 
       Scope = string.Join(" ", Scopes), 
       //append user to url 
       LoginHint = userId, 
       RedirectUri = redirectUri 
      }; 
     } 
    } 
} 
+0

고맙습니다. 시간을 엄청나게 절약 했어. 구글이 처음부터 제대로 처리하지 못했다. –

0

.NET 라이브러리 용 at the source code을 보면이 매개 변수는 지원되지 않습니다.

다음 쿼리 문자열 매개 변수

현재 권한 부여 요청 URL에 정의되어

실제로 GoogleAuthorizationCodeRequestUrl 그것뿐 전화 않습니다 CreateAuthorizationCodeRequest (브로커 내 AuthorizeAsyncCore에 의해 호출 됨) GoogleAuthorizationCodeFlow 클래스, 메소드 내에서
[Google.Apis.Util.RequestParameterAttribute("response_type", Google.Apis.Util.RequestParameterType.Query)] 

[Google.Apis.Util.RequestParameterAttribute("client_id", Google.Apis.Util.RequestParameterType.Query)] 

[Google.Apis.Util.RequestParameterAttribute("redirect_uri", Google.Apis.Util.RequestParameterType.Query)] 

[Google.Apis.Util.RequestParameterAttribute("scope", Google.Apis.Util.RequestParameterType.Query)] 

[Google.Apis.Util.RequestParameterAttribute("state", Google.Apis.Util.RequestParameterType.Query)] 

ClientId, ScopeRedirectUrl을 설정합니다.

해당 클래스에 추가 등록 정보를 설정하려면 브로커 서비스 인스턴스를 직접 작성해야합니다.

+0

괜찮 힌트 주셔서 감사합니다,하지만 속성 "login_hint"는 [소스]에있다 https://code.google.com/p/google- api-dotnet-client/source/browse/Src/GoogleApis.Auth/OAuth2/Requests/GoogleAuthorizationCodeRequestUrl.cs). 하지만 GoogleWebAuthorizationBroker에서 어떻게 사용할 수 있습니까? – Ronny

+1

브로커 도우미 클래스를 사용하는 대신 GoogleAuthorizationCodeRequest를 기반으로 자신의 요청을 공식화해야합니다. –

+1

힌트를 보내 주셔서 감사합니다! – Ronny