示例#1
0
        protected IShareFileClient GetShareFileClient()
        {
            try
            {
                using (var fileStream = System.IO.File.OpenRead("TestConfig.json"))
                    using (var streamReader = new StreamReader(fileStream))
                    {
                        var info     = streamReader.ReadToEnd();
                        var userInfo = JsonConvert.DeserializeObject <UserInfo>(info);

                        var sfClient = new ShareFileClient(userInfo.GetBaseUri().ToString());

                        lock (oauthTokenLock)
                        {
                            if (token == null)
                            {
                                var oauthService = new OAuthService(sfClient, userInfo.ClientId, userInfo.ClientSecret);
                                token = oauthService.GetPasswordGrantRequestQuery(userInfo.Email, userInfo.Password, userInfo.Subdomain, userInfo.Domain).Execute();
                            }
                        }

                        sfClient.BaseUri = token.GetUri();
                        sfClient.AddOAuthCredentials(token);
                        return(sfClient);
                    }
            }
            catch (Exception exception)
            {
                Assert.Inconclusive(string.Format("No UserInfo found in TestConfig.json. Exception: {0}", exception));
                throw;
            }
        }
        /// <summary>
        /// </summary>
        /// <param name="oauthToken"></param>
        public void AddOAuthCredentials(OAuthToken oauthToken)
        {
            var host = oauthToken.GetUri();

            Logging.Info("Adding OAuth Credentials using oauthToken");
            Logging.Debug("Host: {0}", new object[] { host });

            try
            {
                var existingCredentials = CredentialCache.GetCredential(host, "Bearer");
                if (existingCredentials != null)
                {
                    CredentialCache.Remove(host, "Bearer");
                }
            }
            catch (Exception exception)
            {
                Logging.Error(exception, "Failed to add OAuth credentials");
            }
            finally
            {
                CredentialCache.Add(host, "Bearer", new OAuth2Credential(oauthToken));
            }
        }