示例#1
0
        public async Task <CodeAuthRequest> GetAccessTokenAsync(int clientId, string clientSecret, string redirectUri, string code)
        {
            var sb  = new StringBuilder($"https://oauth.vk.com/access_token?client_id={clientId}");
            var uri = string.IsNullOrWhiteSpace(redirectUri) ? "https://oauth.vk.com/blank.html" : redirectUri;

            sb.Append($"&client_secret={clientSecret}");
            sb.Append($"&redirect_uri={uri}");
            sb.Append($"&code={code}");

            using (var client = new HttpClient())
            {
                var response = await(await client.GetAsync(sb.ToString()).ConfigureAwait(false))
                               .Content.ReadAsStringAsync().ConfigureAwait(false);

                var parsedResponse = JObject.Parse(response);

                if (parsedResponse["error"] != null)
                {
                    return(new CodeAuthRequest
                    {
                        Error = CitrinaJsonConverter.Deserialize <CodeAuthError>(response),
                        IsError = true
                    });
                }

                var token = CitrinaJsonConverter.Deserialize <CodeAuthResponse>(response);
                return(new CodeAuthRequest
                {
                    AccessToken = new UserAccessToken(token.AccessToken, token.ExpiresIn, token.UserId, clientId)
                });
            }
        }
示例#2
0
        public static async Task <UploadRequest <T> > UploadAsync <T>(string url, IEnumerable <string> files, string contentFieldName, bool incrementalFieldName = false)
        {
            var uploadingFiles = await GetFilesContentAsync(files).ConfigureAwait(false);

            if (uploadingFiles == null)
            {
                return(new UploadRequest <T>
                {
                    IsError = true,
                    Error = "No content found."
                });
            }

            using (var client = new HttpClient())
            {
                using (var content = new MultipartFormDataContent("---------------CitrinaBoundary"))
                {
                    for (int i = 0; i < uploadingFiles.Length; i++)
                    {
                        var fileContent = new ByteArrayContent(uploadingFiles[i].Content);
                        fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("file")
                        {
                            FileName = uploadingFiles[i].Name,
                            Name     = incrementalFieldName ? $"{contentFieldName}{i + 1}" : $"{contentFieldName}"
                        };

                        content.Add(fileContent);
                    }

                    using (var response = await client.PostAsync(url, content).ConfigureAwait(false))
                    {
#if (!NET45)
                        Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
#endif
                        var data = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                        try
                        {
                            return(new UploadRequest <T>
                            {
                                Response = CitrinaJsonConverter.Deserialize <T>(data)
                            });
                        }
                        catch (Exception e)
                        {
                            return(new UploadRequest <T>
                            {
                                IsError = true,
                                Error = $"Error occured while uploading files: Type={e.GetType()}, Message={e.Message}"
                            });
                        }
                    }
                }
            }
        }
 public T GetEventObject <T>(CallbackEvent e)
     where T : ICallbackModel
 {
     try
     {
         //TODO: Need serialization
         return(CitrinaJsonConverter.Deserialize <T>(/*e.Object*/ null));
     }
     catch
     {
         return(default(T));
     }
 }
示例#4
0
        private static void ObtainDeserializedResponse <TResponse>(InternalApiRequest <TResponse> request, string response)
        {
            if (string.IsNullOrWhiteSpace(response))
            {
                request.Error = new ApiError(EmptyResponseErrorText);
            }

            var    parsedResponse = JObject.Parse(response);
            JToken errorToken, responseToken;

            if ((errorToken = parsedResponse["error"]) != null)
            {
                var error = (ErrorResponse)CitrinaJsonConverter.Deserialize(errorToken.ToString(), typeof(ErrorResponse));
                request.Error = new ApiError(error.Code, error.Message, error.RequestParameters?.ToDictionary(k => k.Key, v => v.Value));
            }
            else if ((responseToken = parsedResponse["response"]) != null)
            {
                request.Response = (TResponse)CitrinaJsonConverter.Deserialize(responseToken.ToString(), typeof(TResponse));
            }
        }