在c# MVC中从Google+登录迁移到Google登录的方法如下:
string clientId = "YOUR_CLIENT_ID";
string redirectUri = "YOUR_REDIRECT_URI";
string authorizationUrl = $"https://accounts.google.com/o/oauth2/v2/auth?client_id={clientId}&redirect_uri={redirectUri}&response_type=code&scope=openid%20email%20profile";
string code = Request.QueryString["code"];
string tokenUrl = "https://accounts.google.com/o/oauth2/token";
string accessToken = "";
using (HttpClient client = new HttpClient())
{
var tokenResponse = await client.PostAsync(tokenUrl, new StringContent($"code={code}&client_id={clientId}&redirect_uri={redirectUri}&grant_type=authorization_code", Encoding.UTF8, "application/x-www-form-urlencoded"));
var tokenJson = await tokenResponse.Content.ReadAsStringAsync();
var tokenData = JsonConvert.DeserializeObject<dynamic>(tokenJson);
accessToken = tokenData.access_token;
}
string peopleApiUrl = "https://people.googleapis.com/v1/people/me?personFields=names,emailAddresses";
string userInfo = "";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var userInfoResponse = await client.GetAsync(peopleApiUrl);
userInfo = await userInfoResponse.Content.ReadAsStringAsync();
}
需要注意的是,以上代码只是一个示例,实际上,你可能还需要处理错误、存储和验证访问令牌等其他方面的逻辑。
对于c# MVC中的Google登录迁移,推荐使用腾讯云的云开发服务,该服务提供了与各种云端资源的连接和集成,以帮助你构建更加灵活和安全的应用程序。腾讯云云开发产品详情请参考:https://cloud.tencent.com/product/tcb
领取专属 10元无门槛券
手把手带您无忧上云