コード例 #1
0
 void Initialize(
     string providerName,
     string authorizationUrl,
     string accessTokenUrl,
     string client_id,
     string client_secret,
     string redirect_uri,
     string scope              = null,
     string userInfoEndpoint   = null,
     string openIdDiscoveryUrl = null)
 {
     State = new OAuth2NetState
     {
         ProviderName        = providerName,
         AuthorizationUrl    = authorizationUrl,
         AccessTokenUrl      = accessTokenUrl,
         ClientId            = client_id,
         ClientSecret        = client_secret,
         RedirectUri         = redirect_uri,
         Scope               = scope,
         AuthorizationParams = AuthorizationParams.Any()
             ? string.Join("&", AuthorizationParams.Select(p => p.Key + "=" + Uri.EscapeDataString(p.Value)))
             : null,
         UserInfoEndpoint   = userInfoEndpoint,
         OpenIdDiscoveryUrl = openIdDiscoveryUrl
     };
 }
コード例 #2
0
        public static void SuccessCallback(OAuth2NetState state)
        {
            using (var cli = NewAuthorizedClient(state.AccessTokenType, state.AccessToken,
                                                 accept: "application/json"))
            {
                var json = cli.DownloadString(state.UserInfoEndpoint);
                var data = JObject.Parse(json);

                state.PersonId         = data["sub"]?.Value <string>();
                state.PersonName       = data["name"]?.Value <string>();
                state.PersonEmail      = data["email"]?.Value <string>();
                state.PersonPhotoUrl   = data["picture"]?.Value <string>();
                state.PersonProfileUrl = data["profile"]?.Value <string>();
                state.PersonLocale     = data["locale"]?.Value <string>();
            }
        }
コード例 #3
0
        public static void SuccessCallback(OAuth2NetState state)
        {
            using (var cli = NewAuthorizedClient(state.AccessTokenType, state.AccessToken,
                                                 accept: "application/json"))
            {
                var json = cli.DownloadString($"https://graph.facebook.com/v3.3/me?access_token={state.AccessToken}&fields=id,name,email");
                var data = JObject.Parse(json);

                state.PersonId    = data["id"]?.Value <string>();
                state.PersonName  = data["name"]?.Value <string>();
                state.PersonEmail = data["email"]?.Value <string>();

                json = cli.DownloadString($"https://graph.facebook.com/{state.PersonId}/picture?type=large&redirect=false");
                data = JObject.Parse(json);

                state.PersonPhotoUrl = data["data"]?["url"]?.Value <string>();
            }
        }
コード例 #4
0
ファイル: OAuth2GitHub.cs プロジェクト: obarlik/OAuth2Net
        public static void SuccessCallback(OAuth2NetState state)
        {
            using (var cli = NewAuthorizedClient(state.AccessTokenType, state.AccessToken,
                                                 accept: "application/vnd.github.machine-man-preview+json"))
            {
                var jsonText = cli.DownloadString("https://api.github.com/user");
                var json     = JObject.Parse(jsonText);

                state.PersonId         = json["id"]?.Value <string>();
                state.PersonName       = json["name"]?.Value <string>() ?? json["login"]?.Value <string>();
                state.PersonEmail      = json["email"]?.Value <string>();
                state.PersonPhotoUrl   = json["avatar_url"]?.Value <string>();
                state.PersonProfileUrl = json["html_url"]?.Value <string>();
                state.PersonLocation   = json["location"]?.Value <string>();
                state.PersonInfo       = json["bio"]?.Value <string>();
            }


            if (string.IsNullOrWhiteSpace(state.PersonEmail))
            {
                using (var cli = NewAuthorizedClient(state.AccessTokenType, state.AccessToken,
                                                     accept: "application/vnd.github.machine-man-preview+json"))
                {
                    try
                    {
                        var text  = cli.DownloadString("https://api.github.com/user/emails");
                        var mails = JArray.Parse(text);

                        state.PersonEmail =
                            mails.Where(m => m["primary"]?.Value <bool>() ?? false)
                            .Select(m => m["email"]?.Value <string>())
                            .FirstOrDefault();
                    }
                    catch
                    {
                        // tried
                    }
                }
            }
        }
コード例 #5
0
        public static void SuccessCallback(OAuth2NetState state)
        {
            using (var cli = NewAuthorizedClient(state.AccessTokenType, state.AccessToken))
            {
                var json = cli.DownloadString(
                    "https://api.linkedin.com/v2/me" +
                    "?projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))");

                var profileJson = JObject.Parse(json);

                state.PersonId = profileJson["id"]?.Value <string>();

                state.PersonName =
                    profileJson["firstName"]?["localized"]?.First?.First?.Value <string>()
                    + " " + profileJson["lastName"]?["localized"]?.First?.First?.Value <string>();

                state.PersonPhotoUrl =
                    profileJson["profilePicture"]?["displayImage~"]?["elements"]?
                    .Select(el =>
                            new
                {
                    width = el["data"]?["com.linkedin.digitalmedia.mediaartifact.StillImage"]?["storageSize"]?["width"]?.Value <int>(),
                    url   = el["identifiers"]?.First?["identifier"]?.Value <string>()
                })
                    .OrderByDescending(el => el.width)
                    .Select(el => el.url)
                    .FirstOrDefault();

                json = cli.DownloadString(
                    "https://api.linkedin.com/v2/emailAddress" +
                    "?q=members&projection=(elements*(handle~))");

                var emailJson = JObject.Parse(json);

                state.PersonEmail =
                    emailJson["elements"].First?["handle~"]?["emailAddress"]?.Value <string>();
            }
        }