【问题标题】:Google authentication using azure services使用 Azure 服务进行 Google 身份验证
【发布时间】:2015-08-11 20:51:52
【问题描述】:

我在 Visual Studio (c#) 中使用 azure services 验证 Google 帐户

我从 Google 帐户获得了访问令牌

如何从该访问令牌中获取用户信息?

我的服务器端代码

var user = User as ServiceUser;
var identities = await user.GetIdentitiesAsync();
var googleCredential = identities[0] as Microsoft.WindowsAzure.Mobile.Service.Security.GoogleCredentials;
var accessToken = googleCredential.AccessToken;

【问题讨论】:

    标签: c# visual-studio azure-mobile-services google-authentication


    【解决方案1】:

    您必须使用客户令牌直接与 Google API 对话以获取有关该用户的信息。

    这是博客文章的链接:http://blogs.msdn.com/b/carlosfigueira/archive/2012/10/25/getting-user-information-on-azure-mobile-services.aspx

    这里是 Google API 的链接:https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#windows-phone-81

    这是一个带有 Node.js 后端的示例:

    function insert(item, user, request) {
        item.UserName = "<unknown>"; // default
        user.getIdentities({
            success: function (identities) {
                var req = require('request');
                if (identities.google) {
                    var googleAccessToken = identities.google.accessToken;
                    var url = 'https://www.googleapis.com/oauth2/v3/userinfo?access_token=' + googleAccessToken;
                    req(url, function (err, resp, body) {
                        if (err || resp.statusCode !== 200) {
                            console.error('Error sending data to Google API: ', err);
                            request.respond(statusCodes.INTERNAL_SERVER_ERROR, body);
                        } else {
                            try {
                                var userData = JSON.parse(body);
                                item.UserName = userData.name;
                                request.execute();
                            } catch (ex) {
                                console.error('Error parsing response from Google API: ', ex);
                                request.respond(statusCodes.INTERNAL_SERVER_ERROR, ex);
                            }
                        }
                    });
                } else {
                    // Insert with default user name
                    request.execute();
                }
            }
        });
    }
    

    【讨论】:

    • 有没有其他方法可以通过服务器端控制器获取用户信息表单访问令牌??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    • 2017-09-06
    • 1970-01-01
    • 2014-06-04
    • 2020-05-21
    相关资源
    最近更新 更多