Пример #1
0
        /// <summary>
        /// Get access token by auth code
        /// </summary>
        /// <param name="authCode">Authorization code after user approved</param>
        /// <exception cref="ApiException"></exception>
        /// <returns></returns>
        public async Task <AccessToken> GetAccessTokenAsync(string authCode)
        {
            var content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair <string, string>("grant_type", "authorization_code"),
                new KeyValuePair <string, string>("code", authCode),
                new KeyValuePair <string, string>("redirect_uri", _redirectUrl.OriginalString),
                new KeyValuePair <string, string>("client_id", _clientId),
                new KeyValuePair <string, string>("client_secret", _clientSecret)
            });

            var requestMessage = new HttpRequestMessage(HttpMethod.Post, $"{_authHost}oauth/v2/accessToken")
            {
                Version = HttpVersion.Version11,
                Content = content,
            };

            var response = await _client.SendAsync(requestMessage);

            if (!response.IsSuccessStatusCode)
            {
                throw new ApiException(ExceptionModel.FromJson(await response.Content.ReadAsStringAsync()));
            }

            var accessToken = AccessToken.FromJson(await response.Content.ReadAsStringAsync());

            AccessToken = accessToken;
            CheckTokenThenAddToHeaders();

            return(accessToken);
        }
Пример #2
0
        private async Task <Share> PostShareAsync(Share share)
        {
            CheckTokenThenAddToHeaders();
            var content  = new StringContent(share.ToJson(), Encoding.UTF8, "application/json");
            var response = await _client.PostAsync($"{_apiHost}v2/shares", content);

            var responseJson = await response.Content.ReadAsStringAsync();

            if (!response.IsSuccessStatusCode)
            {
                throw new ApiException(ExceptionModel.FromJson(responseJson));
            }

            return(Share.FromJson(responseJson));
        }
Пример #3
0
        private async Task <EntityElements <Share> > GetPostsAsync(string ownerURN, int sharesPerOwner = 100)
        {
            CheckTokenThenAddToHeaders();
            var response = await _client.GetAsync($"{_apiHost}v2/shares?q=owners&owners={ownerURN}&sharesPerOwner={sharesPerOwner}");

            var responseJson = await response.Content.ReadAsStringAsync();

            var shares = EntityElements <Share> .FromJson(responseJson);

            if (!response.IsSuccessStatusCode)
            {
                throw new ApiException(ExceptionModel.FromJson(responseJson));
            }

            return(shares);
        }
Пример #4
0
        /// <summary>
        /// Get information about authorized user, if your access token invalid you will get ApiException error
        /// </summary>
        /// <exception cref="ApiException"></exception>
        /// <returns></returns>
        public async Task <Profile> GetOwnProfileAsync()
        {
            CheckTokenThenAddToHeaders();
            var response = await _client.GetAsync($"{_apiHost}v2/me");

            var jsonData = await response.Content.ReadAsStringAsync();

            var profile = Profile.FromJson(jsonData);

            if (!response.IsSuccessStatusCode)
            {
                throw new ApiException(ExceptionModel.FromJson(await response.Content.ReadAsStringAsync()));
            }

            return(profile);
        }
Пример #5
0
        /// <summary>
        /// Get list of companies which authorized user have approved ADMINISTRATOR role on these companies, required r_organization permission
        /// </summary>
        /// <exception cref="ApiException"></exception>
        /// <returns></returns>
        public async Task <Organization[]> GetCompaniesAsync()
        {
            CheckTokenThenAddToHeaders();
            var response = await _client.GetAsync($"{_apiHost}v2/organizationalEntityAcls?q=roleAssignee&role=ADMINISTRATOR");

            var responseJson = await response.Content.ReadAsStringAsync();

            if (!response.IsSuccessStatusCode)
            {
                throw new ApiException(ExceptionModel.FromJson(await response.Content.ReadAsStringAsync()));
            }

            var organizationalEntityAcls = EntityElements <OrganizationalAccess> .FromJson(responseJson);

            var ownCompaniesId = new List <long>();

            foreach (var element in organizationalEntityAcls.Elements)
            {
                if (element.Role == "ADMINISTRATOR" && element.State == "APPROVED")
                {
                    long id = long.Parse(element.OrganizationalTarget.Split(':').Last());
                    ownCompaniesId.Add(id);
                }
            }

            var companiesList = new List <Organization>();

            foreach (var companyId in ownCompaniesId)
            {
                response = await _client.GetAsync($"{_apiHost}v2/organizations/{companyId}");

                if (!response.IsSuccessStatusCode)
                {
                    throw new ApiException(ExceptionModel.FromJson(await response.Content.ReadAsStringAsync()));
                }

                responseJson = await response.Content.ReadAsStringAsync();

                companiesList.Add(Organization.FromJson(responseJson));
            }

            return(companiesList.ToArray());
        }
Пример #6
0
        /// <summary>
        /// To upload rich media, attach the media as multipart/form-data. Currently, only jpeg and png formats are supported. The media file size should be under 10 MB for all file types. All image files have a pixel restriction of 40,000,000, and you may only upload one media object per API call.
        /// </summary>
        /// <param name="filePath">Path to jpeg or png file</param>
        /// <returns>The location of uploaded rich media (Location property of <c>RichMedia</c> class</returns>
        public async Task <RichMedia> UploadRichMediaAsync(string filePath)
        {
            CheckTokenThenAddToHeaders();
            var content = new MultipartFormDataContent();

            using (var file = new FileStream(path: filePath, mode: FileMode.Open))
            {
                content.Add(new StreamContent(file), "fileupload");
                var response = await _client.PostAsync($"{_apiHost}media/upload", content);

                var responseJson = await response.Content.ReadAsStringAsync();

                if (!response.IsSuccessStatusCode)
                {
                    throw new ApiException(ExceptionModel.FromJson(responseJson));
                }

                return(RichMedia.FromJson(responseJson));
            }
        }