检索Google驱动器登录的用户凭证 [英] retrieve the user-credential of Google drive login
本文介绍了检索Google驱动器登录的用户凭证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要检索Google硬盘登录的用户凭据。假设我有一个在谷歌驱动器的帐户,我想检索用户信息 - 我怎样才能在C#中工作?
当我运行下面的代码时,错误:
using System;
使用System.Diagnostics;
使用DotNetOpenAuth.OAuth2;
使用Google.Apis.Authentication.OAuth2;
使用Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
使用Google.Apis.Drive.v2;
使用Google.Apis.Drive.v2.Data;
使用Google.Apis.Util;
使用Google.Apis.Requests;
使用System.Collections.Generic;
使用System.Net;
使用Google.GData.Client;
使用Google.GData.Documents;
使用Google.GData.Extensions;
使用Google.Apis.Oauth2.v2;
使用Google.Apis.Oauth2.v2.Data;
使用Google.Apis.Authentication;
namespace google_drive_iterate_folders
{
class Program
{
static DriveService BuildService(IAuthenticator凭证)
{
return new DriveService(证书);
$ b public static IAuthenticator GetCredentials(String authorizationCode,String state)
{
String emailAddress =;
尝试
{
IAuthorizationState凭证= ExchangeCode(authorizationCode);
UserInfo userInfo = GetUserInfo(credentials);
字符串userId = userInfo.Id;
emailAddress = userInfo.Email;
if(!String.IsNullOrEmpty(credentials.RefreshToken))
{
StoreCredentials(userId,credentials);
返回GetAuthenticatorFromState(凭证);
}
else
{
credentials = GetStoredCredentials(userId);
if(credentials!= null&&
!String.IsNullOrEmpty(credentials.RefreshToken))
{
return GetAuthenticatorFromState(credentials);
$ b catch(Exception ex)
{
Console.WriteLine(代码交换期间发生错误);
//驱动器应用程序应尝试检索
//当前会话的用户和凭据。
//如果没有可用的,将用户重定向到授权URL。
e.AuthorizationUrl = GetAuthorizationUrl(emailAddress,state);
throw e;
}
// catch()
// {
// Console.WriteLine(不能检索用户ID);
//}
//未检索到刷新标记。
String authorizationUrl = GetAuthorizationUrl(emailAddress,state);
抛出新的NoRefreshTokenException(authorizationUrl);
}
///< summary>
///交换OAuth 2.0凭据的授权码。
///< / summary>
///< param name =authorizationCode>为OAuth 2.0凭证交换
////的授权码< / param>
///<返回> OAuth 2.0凭证。< / returns>
///< exception cref =CodeExchangeException>发生错误。< / exception>
static IAuthorizationState ExchangeCode(String authorizationCode)
{
var provider = new NativeApplicationClient(
GoogleAuthenticationServer.Description,
ClientCredentials.CLIENT_ID,
ClientCredentials.CLIENT_SECRET) ;
IAuthorizationState state = new AuthorizationState();
state.Callback = new Uri(ClientCredentials.REDIRECT_URI);
尝试
{
state = provider.ProcessUserAuthorization(authorizationCode,state);
返回状态;
}
catch(ProtocolException)
{
throw new CodeExchangeException(null);
}
}
///< summary>
///向用户信息API发送请求以检索用户的信息。
///< / summary>
///< param name =credentials> OAuth 2.0凭证授权
////请求。< / param>
///<返回>用户信息。< / returns>
///< exception cref =NoUserIdException>发生错误。< / exception>
静态Userinfo GetUserInfo(IAuthenticator凭证)
{
Oauth2Service userInfoService = new Oauth2Service(凭证);
Userinfo userInfo = null;
try
{
userInfo = userInfoService.Userinfo.Get()。Fetch();
}
catch(GoogleApiRequestException e)
{
Console.WriteLine(发生错误:+ e.Message); (userInfo!= null&&!String.IsNullOrEmpty(userInfo.Id))
{
return userInfo;
}
if
}
else
{
throw new NoUserIdException();
}
}
///< summary>
///使用提供的状态检索IAuthenticator实例。
///< / summary>
///< param name =credentials>要使用的OAuth 2.0凭证。< / param>
///<返回>使用提供的OAuth 2.0
/// credentials< / returns>的认证器
public static IAuthenticator GetAuthenticatorFromState(
IAuthorizationState凭证)
{
var provider = new StoredStateClient(
GoogleAuthenticationServer.Description,ClientCredentials.CLIENT_ID,
ClientCredentials.CLIENT_SECRET ,证书);
var auth = new OAuth2Authenticator< StoredStateClient>(
provider,StoredStateClient.GetState ..
查看此项: - https://developers.google.com/drive/examples/dotnet
I need to retrieve the user-credential of Google drive login. Suppose I have an account in google drive and I want to retrieve the user info - how can I get this working in C#?
When I run the code below, I get errors:
using System;
using System.Diagnostics;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;
using Google.Apis.Util;
using Google.Apis.Requests;
using System.Collections.Generic;
using System.Net;
using Google.GData.Client;
using Google.GData.Documents;
using Google.GData.Extensions;
using Google.Apis.Oauth2.v2;
using Google.Apis.Oauth2.v2.Data;
using Google.Apis.Authentication;
namespace google_drive_iterate_folders
{
class Program
{
static DriveService BuildService(IAuthenticator credentials)
{
return new DriveService(credentials);
}
public static IAuthenticator GetCredentials(String authorizationCode, String state)
{
String emailAddress = "";
try
{
IAuthorizationState credentials = ExchangeCode(authorizationCode);
Userinfo userInfo = GetUserInfo(credentials);
String userId = userInfo.Id;
emailAddress = userInfo.Email;
if (!String.IsNullOrEmpty(credentials.RefreshToken))
{
StoreCredentials(userId, credentials);
return GetAuthenticatorFromState(credentials);
}
else
{
credentials = GetStoredCredentials(userId);
if (credentials != null &&
!String.IsNullOrEmpty(credentials.RefreshToken))
{
return GetAuthenticatorFromState(credentials);
}
}
}
catch (Exception ex)
{
Console.WriteLine("An error occurred during code exchange.");
// Drive apps should try to retrieve the user and credentials for
// the current session.
// If none is available, redirect the user to the authorization URL.
e.AuthorizationUrl = GetAuthorizationUrl(emailAddress, state);
throw e;
}
//catch ()
//{
// Console.WriteLine("No user ID could be retrieved.");
//}
// No refresh token has been retrieved.
String authorizationUrl = GetAuthorizationUrl(emailAddress, state);
throw new NoRefreshTokenException(authorizationUrl);
}
/// <summary>
/// Exchange an authorization code for OAuth 2.0 credentials.
/// </summary>
/// <param name="authorizationCode">Authorization code to exchange
//// for OAuth 2.0 credentials.</param>
/// <returns>OAuth 2.0 credentials.</returns>
/// <exception cref="CodeExchangeException">An error occurred.</exception>
static IAuthorizationState ExchangeCode(String authorizationCode)
{
var provider = new NativeApplicationClient(
GoogleAuthenticationServer.Description,
ClientCredentials.CLIENT_ID,
ClientCredentials.CLIENT_SECRET);
IAuthorizationState state = new AuthorizationState();
state.Callback = new Uri(ClientCredentials.REDIRECT_URI);
try
{
state = provider.ProcessUserAuthorization(authorizationCode, state);
return state;
}
catch (ProtocolException)
{
throw new CodeExchangeException(null);
}
}
/// <summary>
/// Send a request to the User Info API to retrieve the user's information.
/// </summary>
/// <param name="credentials">OAuth 2.0 credentials to authorize
//// the request.</param>
/// <returns>User's information.</returns>
/// <exception cref="NoUserIdException">An error occurred.</exception>
static Userinfo GetUserInfo(IAuthenticator credentials)
{
Oauth2Service userInfoService = new Oauth2Service(credentials);
Userinfo userInfo = null;
try
{
userInfo = userInfoService.Userinfo.Get().Fetch();
}
catch (GoogleApiRequestException e)
{
Console.WriteLine("An error occurred: " + e.Message);
}
if (userInfo != null && !String.IsNullOrEmpty(userInfo.Id))
{
return userInfo;
}
else
{
throw new NoUserIdException();
}
}
/// <summary>
/// Retrieve an IAuthenticator instance using the provided state.
/// </summary>
/// <param name="credentials">OAuth 2.0 credentials to use.</param>
/// <returns>Authenticator using the provided OAuth 2.0
/// credentials</returns>
public static IAuthenticator GetAuthenticatorFromState(
IAuthorizationState credentials)
{
var provider = new StoredStateClient(
GoogleAuthenticationServer.Description, ClientCredentials.CLIENT_ID,
ClientCredentials.CLIENT_SECRET, credentials);
var auth = new OAuth2Authenticator<StoredStateClient>(
provider, StoredStateClient.GetState..
解决方案
Google Drive SDK documentation includes a complete .NET sample that shows how to store credentials in a database and restore them when needed:
Check this out:- https://developers.google.com/drive/examples/dotnet
这篇关于检索Google驱动器登录的用户凭证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文