Example #1
0
        public void SendRequest(RestRequest request, RequestBack callBack)
        {
            Action<string> errAction = (e1) =>
            {
                if (null != callBack)
                {
                    DoubanSdkErrCode sdkErr = DoubanSdkErrCode.XPARAM_ERR;
                    callBack(new DoubanSdkResponse
                    {
                        //requestID = null != data ? data.requestId : "",
                        errCode = sdkErr,
                        specificCode = "",
                        content = e1,
                        stream = null
                    });
                }
            };

            if (null == request)
            {
                errAction("request should`t be null.");
                return;
            }

            m_Client.Authority = ConstDefine.PublicApiUrl;
            m_Client.HasElevatedPermissions = true;

            //添加鉴权
            request.DecompressionMethods = DecompressionMethods.GZip;
            request.Encoding = Encoding.UTF8;

            IWebCredentials credentials = null;

            if (null != DoubanAPI.DoubanInfo.tokenInfo && !string.IsNullOrEmpty(DoubanAPI.DoubanInfo.tokenInfo.access_token))
            {
                request.AddHeader("Authorization", string.Format("Bearer {0}", DoubanAPI.DoubanInfo.tokenInfo.access_token));
            }
            else
            {
                request.AddParameter("source", DoubanSdkData.AppKey);
            }
            request.AddParameter("curtime", DateTime.Now.ToString());
            request.Credentials = credentials;

            m_Client.BeginRequest(request, (e1, e2, e3) => AsyncCallback(e1, e2, callBack));
        }
        public virtual RestRequest PrepareEchoRequest(string realm = "http://api.twitter.com")
        {
            var args = new FunctionArguments
            {
                ConsumerKey = _consumerKey,
                ConsumerSecret = _consumerSecret,
                Token = _token,
                TokenSecret = _tokenSecret
            };

            var request = _protectedResourceQuery.Invoke(args);
            request.Method = WebMethod.Get;
            request.Path = string.Concat("account/verify_credentials", FormatAsString);

            var credentials = (OAuthCredentials)request.Credentials;
            var url = request.BuildEndpoint(_client);
            var workflow = new OAuthWorkflow(credentials);

            var method = request.Method.HasValue ? request.Method.Value : WebMethod.Get;
            var info = workflow.BuildProtectedResourceInfo(method, request.GetAllHeaders(), url);
            var query = credentials.GetQueryFor(url, request, info, method, TraceEnabled);

            ((OAuthWebQuery)query).Realm = realm;
            var auth = query.GetAuthorizationContent();

            var echo = new RestRequest();
            echo.AddHeader("X-Auth-Service-Provider", url);
            echo.AddHeader("X-Verify-Credentials-Authorization", auth);
            return echo;
        }
Example #3
0
        static void SendRegistrationToServer(IEnumerable<RegistrationInfo> regs)
        {
            // I'm just so sorry about all this crap.
            string separator = "¬";
            string urls = "", tokens = "", names = "", types = "";
            string version = WPVersion == OSVersion.WP8 ? "8" : "7";

            foreach (var reg in regs)
            {
                urls += reg.ChannelUri + separator;
                names += reg.User.ScreenName + separator;
                tokens += Library.Encrypting.EncodeTokens(reg.User.Key, reg.User.Secret) + separator;
                types += reg.Type + separator;
            }

            // Remove last separator
            urls.Substring(0, urls.Length - 1);
            tokens.Substring(0, tokens.Length - 1);
            names.Substring(0, names.Length - 1);
            types.Substring(0, types.Length - 1);

            string postContents = "{\"AccessTokens\" : \"" + tokens + "\",\"PushUris\" : \"" + urls + "\",\"Usernames\" : \"" + names + "\",\"Types\" : \"" + types + "\",\"OSVersion\" : \"" + version + "\"}";

            var request = new RestRequest();
            request.Path = Library.SensitiveData.PushRegisterPostUriFormat;
            request.Method = WebMethod.Post;
            request.AddHeader("Content-Type", "application/json");
            request.AddPostContent(Encoding.UTF8.GetBytes(postContents));

            try
            {
                new RestClient().BeginRequest(request, (req, resp, s) =>
                {
                    ReportRegisterToUser(resp);
                });
            }
            catch (Exception)
            {
            }
        }