Google+的API:我如何使用RefreshTokens避免请访问每次我的应用程序启动? [英] Google+ API: How can I use RefreshTokens to avoid requesting access every time my app launches?
问题描述
我尝试使用Google+的API访问的身份验证的用户信息。我已经复制的样本,它工作正常(下图)的一个约code,但我无法使它的方式工作,我可以重用跨应用程序,启动令牌。
我试图捕捉RefreshToken属性,并使用 provider.RefreshToken()
(除其他事项外),并始终得到 400错误的请求
响应。
有谁知道如何使这项工作,或者知道哪里可以找到一些样品?该谷歌code网站没有按'不像是会掩盖这一点: - (
类节目
{
私人常量字符串范围=https://www.googleapis.com/auth/plus.me;
静态无效的主要(字串[] args)
{
VAR提供商=新NativeApplicationClient(GoogleAuthenticationServer.Description);
provider.ClientIdentifier =嗒嗒;
provider.ClientSecret =嗒嗒;
VAR AUTH =新OAuth2Authenticator< NativeApplicationClient>(供应商,GetAuthentication);
VAR加上=新PlusService(AUTH);
plus.Key =嗒嗒;
。VAR我= plus.People.Get(我)取();
Console.WriteLine(me.DisplayName);
}
私有静态IAuthorizationState GetAuthentication(NativeApplicationClient ARG)
{
//获取身份验证网址:
IAuthorizationState状态=新AuthorizationState(新[] {范围});
state.Callback =新的URI(NativeApplicationClient.OutOfBandCallbackUrl);
乌里authUri = arg.RequestUserAuthorization(州);
//请求从用户授权(通过打开浏览器窗口):
的Process.Start(authUri.ToString());
Console.Write(授权code:);
字符串AUTH code =到Console.ReadLine();
Console.WriteLine();
//使用授权code。获取访问令牌:
返回arg.ProcessUserAuthorization(AUTH code,状态);
}
}
下面是一个例子。确保你添加一个字符串的设置所谓的RefreshToken和参考System.Security程序或找到另一种方式来安全地存储刷新令牌。
私有静态的byte [] aditionalEntropy = {1,2,3,4,5};
私有静态IAuthorizationState GetAuthorization(NativeApplicationClient ARG)
{
//获取身份验证网址:
IAuthorizationState状态=新AuthorizationState(新[] {PlusService.Scopes.PlusMe.GetStringValue()});
state.Callback =新的URI(NativeApplicationClient.OutOfBandCallbackUrl);
字符串refreshToken = LoadRefreshToken();
如果(!String.IsNullOrWhiteSpace(refreshToken))
{
state.RefreshToken = refreshToken;
如果(arg.RefreshToken(州))
返回的状态;
}
乌里authUri = arg.RequestUserAuthorization(州);
//请求从用户授权(通过打开浏览器窗口):
的Process.Start(authUri.ToString());
Console.Write(授权code:);
字符串AUTH code =到Console.ReadLine();
Console.WriteLine();
//使用授权code。获取访问令牌:
VAR的结果= arg.ProcessUserAuthorization(AUTH code,状态);
StoreRefreshToken(州);
返回结果;
}
私人静态字符串LoadRefreshToken()
{
返回Encoding.Uni$c$c.GetString(ProtectedData.Unprotect(Convert.FromBase64String(Properties.Settings.Default.RefreshToken), aditionalEntropy,DataProtectionScope.CurrentUser));
}
私有静态无效StoreRefreshToken(IAuthorizationState州)
{
Properties.Settings.Default.RefreshToken = Convert.ToBase64String(ProtectedData.Protect(Encoding.Uni code.GetBytes(state.RefreshToken),aditionalEntropy,DataProtectionScope.CurrentUser));
Properties.Settings.Default.Save();
}
I'm trying to use the Google+ API to access info for the authenticated user. I've copied some code from one of the samples, which works fine (below), however I'm having trouble making it work in a way I can reuse the token across app-launches.
I tried capturing the "RefreshToken" property and using provider.RefreshToken()
(amongst other things) and always get a 400 Bad Request
response.
Does anyone know how to make this work, or know where I can find some samples? The Google Code site doesn't seem to cover this :-(
class Program
{
private const string Scope = "https://www.googleapis.com/auth/plus.me";
static void Main(string[] args)
{
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
provider.ClientIdentifier = "BLAH";
provider.ClientSecret = "BLAH";
var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthentication);
var plus = new PlusService(auth);
plus.Key = "BLAH";
var me = plus.People.Get("me").Fetch();
Console.WriteLine(me.DisplayName);
}
private static IAuthorizationState GetAuthentication(NativeApplicationClient arg)
{
// Get the auth URL:
IAuthorizationState state = new AuthorizationState(new[] { Scope });
state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
Uri authUri = arg.RequestUserAuthorization(state);
// Request authorization from the user (by opening a browser window):
Process.Start(authUri.ToString());
Console.Write(" Authorization Code: ");
string authCode = Console.ReadLine();
Console.WriteLine();
// Retrieve the access token by using the authorization code:
return arg.ProcessUserAuthorization(authCode, state);
}
}
Here is an example. Make sure you add a string setting called RefreshToken and reference System.Security or find another way to safely store the refresh token.
private static byte[] aditionalEntropy = { 1, 2, 3, 4, 5 };
private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
{
// Get the auth URL:
IAuthorizationState state = new AuthorizationState(new[] { PlusService.Scopes.PlusMe.GetStringValue() });
state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
string refreshToken = LoadRefreshToken();
if (!String.IsNullOrWhiteSpace(refreshToken))
{
state.RefreshToken = refreshToken;
if (arg.RefreshToken(state))
return state;
}
Uri authUri = arg.RequestUserAuthorization(state);
// Request authorization from the user (by opening a browser window):
Process.Start(authUri.ToString());
Console.Write(" Authorization Code: ");
string authCode = Console.ReadLine();
Console.WriteLine();
// Retrieve the access token by using the authorization code:
var result = arg.ProcessUserAuthorization(authCode, state);
StoreRefreshToken(state);
return result;
}
private static string LoadRefreshToken()
{
return Encoding.Unicode.GetString(ProtectedData.Unprotect(Convert.FromBase64String(Properties.Settings.Default.RefreshToken), aditionalEntropy, DataProtectionScope.CurrentUser));
}
private static void StoreRefreshToken(IAuthorizationState state)
{
Properties.Settings.Default.RefreshToken = Convert.ToBase64String(ProtectedData.Protect(Encoding.Unicode.GetBytes(state.RefreshToken), aditionalEntropy, DataProtectionScope.CurrentUser));
Properties.Settings.Default.Save();
}
这篇关于Google+的API:我如何使用RefreshTokens避免请访问每次我的应用程序启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!