2016-10-28 3 views
2

基本的に、私はOAuth、Google、Google Apisについて何かを理解していません。私はAsp.Net Core 1.0でv3ライブラリを使っています。私はGoogle認証を使用します:Google Apiのアクセスと更新トークンの取得

 app.UseGoogleAuthentication(new GoogleOptions 
     { 
      ClientId = Configuration["Google:ClientId"], 
      ClientSecret = Configuration["Google:ClientSecret"], 
      Scope = { 
      "email", 
      "https://www.googleapis.com/auth/userinfo.email", 
      "profile", 
      "https://www.googleapis.com/auth/userinfo.profile", 
      "openid", 
      "https://www.googleapis.com/auth/calendar", 
     }, 
      AccessType = "offline", 
      SaveTokens = true, 
     }); 

それから私はGoogleカレンダーを使いたいです。しかし、私は一貫してAccess/Refreshトークンを取り戻すことができません(正直なところ、私はこれらが何であるか分かりません)。私はGoogleからのカレンダサービスを取り戻すために、このコードを使用します。

private AuthorizationCodeFlow CreateFlow(IEnumerable<string> scopes) 
    { 
     return new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer() 
     { 
      ClientSecrets = new Google.Apis.Auth.OAuth2.ClientSecrets 
      { 
       ClientId = _ClientId, 
       ClientSecret = _ClientSecret 
      }, 
      DataStore = new MemoryDataStore(), 
      Scopes = scopes 
     }); 
    } 

    private async Task<UserCredential> CreateUserCredential(HttpContext context, string providerKey, IEnumerable<string> scopes) 
    { 
     var flow = CreateFlow(scopes); 
     var token = await context.GetGoogleTokenResponse(); 

     UserCredential credential = new UserCredential(flow, providerKey, token); 
     return credential; 
    } 

    public async Task<CalendarService> CreateCalendarServiceAsync(HttpContext context, string providerKey) 
    { 
     if (string.IsNullOrWhiteSpace(providerKey)) return null; 
     if (context == null) return null; 

     var credential = await CreateUserCredential(context, providerKey, new[] { "https://www.googleapis.com/auth/calendar" }); 
     CalendarService service = new CalendarService(new BaseClientService.Initializer 
     { 
      HttpClientInitializer = credential, 
      ApplicationName = _ApplicationName, 
     }); 

     return service; 
    } 

    public static async Task<TokenResponse> GetGoogleTokenResponse(this HttpContext context) 
    { 
     var info = await context.Authentication.GetAuthenticateInfoAsync("Google"); 
     if (info == null) return null; 

     var token = new TokenResponse 
     { 
      AccessToken = info.Properties.Items[".Token.access_token"], 
      RefreshToken = info.Properties.Items[".Token.refresh_token"], 
      TokenType = info.Properties.Items[".Token.token_type"], 
      Issued = DateTime.Parse(info.Properties.Items[".issued"]), 
     }; 

     return token; 
    } 

これは私が現在のアクセス/リフレッシュトークンを取り戻すために方法を知っている唯一の方法です。私は何が欠けている。時にはそれらは存在し、他の時は存在しない。 .Net Core 1.0バージョンのドキュメントは、スリムで最高です。 Asp.Netコア経由でGoogle Apisにアクセスするにはどんな助けになりますか?

答えて

0

すでにSaveTokens = trueを設定しているので、あなたはこのようにaccess_tokenは得ることができます。await context.Authentication.GetTokenAsync("access_token");

ので、以下のようなあなたの方法を変更すると、おそらくあなたの問題を解決:

private async Task<UserCredential> CreateUserCredential(HttpContext context, string providerKey, IEnumerable<string> scopes) 
{ 
    var flow = CreateFlow(scopes); 
    var token = await context.Authentication.GetTokenAsync("access_token"); 

    UserCredential credential = new UserCredential(flow, providerKey, token); 
    return credential; 
} 
関連する問題