Пример #1
0
        private bool PostNewORCIDRequestToORCIDAPI(BO.ORCID.Person person, string loggedInInternalUsername)
        {
            BO.ORCID.REFPermission refPermission = new ProfilesRNSDLL.BLL.ORCID.REFPermission().Get((int)BO.ORCID.REFPermission.REFPermissions.orcid_profile_create);
            string ORCIDCreateProfileURL         = ProfilesRNSDLL.BO.ORCID.Config.ORCID_API_URL_WITH_VERSION + "/" + "orcid-profile";

            var httpWebRequest = WebRequest.Create(ORCIDCreateProfileURL);

            httpWebRequest.ContentType = "application/orcid+xml";
            httpWebRequest.Method      = System.Net.WebRequestMethods.Http.Post.ToString();

            string authToken = BLL.ORCID.OAuth.GetClientToken(refPermission.PermissionScope, loggedInInternalUsername);

            httpWebRequest.Headers.Add("Authorization", " Bearer " + authToken);

            byte[] xmlBytes = Encoding.UTF8.GetBytes(PersonMessage.CreateNewBasicORCIDMessage(person));
            using (var requestStream = httpWebRequest.GetRequestStream())
            {
                requestStream.Write(xmlBytes, 0, xmlBytes.Length);
                requestStream.Close();
            }
            try
            {
                using (WebResponse response = httpWebRequest.GetResponse())
                {
                    // Example of response.Headers["Location"] is 'http://api.orcid.org/0000-0002-4523-3823/orcid-profile'
                    person.ORCID = System.Text.RegularExpressions.Regex.Split(response.Headers["Location"].ToString(), "/")[3];
                }
            }
            catch (WebException en)
            {
                using (WebResponse response = en.Response)
                {
                    HttpWebResponse httpResponse = (HttpWebResponse)response;

                    person.Error += string.Format("Error code: {0}", httpResponse.StatusCode);
                    using (Stream data = response.GetResponseStream())
                    {
                        string text = new StreamReader(data).ReadToEnd();
                        person.Error += text;
                    }
                }
            }
            if (string.IsNullOrEmpty(person.Error))
            {
                if (!person.HasValidORCID)
                {
                    person.Error = "Error! Invalid ORCID value";
                }
            }
            if (person.Error.Contains("Cannot create ORCID"))
            {
                person.Error = "An error occured creating your ORCID. This email address has already been registed with ORCID.";
            }
            person.HasError = !person.Error.Equals(string.Empty);
            return(!person.HasError);
        }
Пример #2
0
 internal static int GetPermissionID(string permissionScope)
 {
     ProfilesRNSDLL.BO.ORCID.REFPermission permission =
         new ProfilesRNSDLL.BLL.ORCID.REFPermission().GetByPermissionScope(permissionScope);
     if (permission.Exists)
     {
         return(permission.PermissionID);
     }
     else
     {
         throw new NotImplementedException("The specified scope (" + permissionScope + ") is unexpected.");
     }
 }
 internal static int GetPermissionID(string permissionScope)
 {
     ProfilesRNSDLL.BO.ORCID.REFPermission permission =
         new ProfilesRNSDLL.BLL.ORCID.REFPermission().GetByPermissionScope(permissionScope);
     if (permission.Exists)
     {
         return permission.PermissionID;
     }
     else
     {
         throw new NotImplementedException("The specified scope (" + permissionScope + ") is unexpected.");
     }
 }
        private bool PostNewORCIDRequestToORCIDAPI(BO.ORCID.Person person, string loggedInInternalUsername)
        {
            BO.ORCID.REFPermission refPermission = new ProfilesRNSDLL.BLL.ORCID.REFPermission().Get((int)BO.ORCID.REFPermission.REFPermissions.orcid_profile_create);
            string ORCIDCreateProfileURL = ProfilesRNSDLL.BO.ORCID.Config.ORCID_API_URL_WITH_VERSION + "/" + "orcid-profile";

            var httpWebRequest = WebRequest.Create(ORCIDCreateProfileURL);
            httpWebRequest.ContentType = "application/orcid+xml";
            httpWebRequest.Method = System.Net.WebRequestMethods.Http.Post.ToString();

            string authToken = BLL.ORCID.OAuth.GetClientToken(refPermission.PermissionScope, loggedInInternalUsername);

            httpWebRequest.Headers.Add("Authorization", " Bearer " + authToken);

            byte[] xmlBytes = Encoding.UTF8.GetBytes(PersonMessage.CreateNewBasicORCIDMessage(person));
            using (var requestStream = httpWebRequest.GetRequestStream())
            {
                requestStream.Write(xmlBytes, 0, xmlBytes.Length);
                requestStream.Close();
            }
            try
            {
                using (WebResponse response = httpWebRequest.GetResponse())
                {
                    // Example of response.Headers["Location"] is 'http://api.orcid.org/0000-0002-4523-3823/orcid-profile'
                    person.ORCID = System.Text.RegularExpressions.Regex.Split(response.Headers["Location"].ToString(), "/")[3];
                }
            }
            catch (WebException en)
            {
                using (WebResponse response = en.Response)
                {
                    HttpWebResponse httpResponse = (HttpWebResponse)response;

                    person.Error += string.Format("Error code: {0}", httpResponse.StatusCode);
                    using (Stream data = response.GetResponseStream())
                    {
                        string text = new StreamReader(data).ReadToEnd();
                        person.Error += text;
                    }
                }
            }
            if (string.IsNullOrEmpty(person.Error))
            {
                if (!person.HasValidORCID)
                {
                    person.Error = "Error! Invalid ORCID value";
                }
            }
            if (person.Error.Contains("Cannot create ORCID"))
            {

                person.Error = "An error occured creating your ORCID. This email address has already been registed with ORCID.";
            }
            person.HasError = !person.Error.Equals(string.Empty);
            return !person.HasError;
        }