在开始工作之前,您应该获得身份验证令牌。为此,您应该创建链接,用户应该打开并访问您的应用程序。比您应该使用稍后获得的代码女巫请求令牌。https://developers.google.com/accounts/docs/OAuth2Login中描述的这种机制
是这样的:
private const string GetTokenUrl = "https://accounts.google.com/o/oauth2/token";
private new bool Auth(bool needUserCredentionals = true)
{
var dic = new Dictionary<string, string>();
dic.Add("grant_type", "authorization_code");
dic.Add("code", ResponseCode);
dic.Add("client_id", ApplicationId);
dic.Add("client_secret", ApplicationSecret);
dic.Add("redirect_uri", HttpUtility.UrlEncode(AppRedirectUrl));
var str = String.Join("&", dic.Select(item => item.Key + "=" + item.Value).ToArray());
var client = new WebClient();
client.Headers.Add("Content-type", "application/x-www-form-urlencoded");
string s;
try { s = client.UploadString(GetTokenUrl, str); }
catch (WebException) { return false; }
catch (Exception) { return false; }
AuthResponse response;
try { response = JsonConvert.DeserializeObject<AuthResponse>(s); }
catch (Exception) { return false; }
Token = response.access_token;
SessionTime = DateTime.Now.Ticks + response.expires_in;
if (needUserCredentionals)
if (!GetUserInfo()) return false;
return true;
}
public class AuthResponse
{
public string access_token { get; set; }
public string token_type { get; set; }
public long expires_in { get; set; }
}
ResponseCode 这是用户从“访问授权页面”重定向后应该捕获的代码但我猜这个方法是 api 2... 也许我错了,谁知道