Example #1
0
    public IEnumerator GetRequestToken(Action <string> result)
    {
        Dictionary <string, string> content = new Dictionary <string, string>();

        content.Add("client_id", "a82cf830df2780731c65783ddb82b207"); //consumerKey
        content.Add("response_type", "code");                         // code
        content.Add("redirect_uri", "");                              // domains //http://note.youdao.com/redirect
        content.Add("state", "");                                     // state
        content.Add("display", "mobile");                             // display

        string url = YNoteUtil.GetURL("https://[baseURL]/oauth/authorize2");

        Log.send(url, content);
        UnityWebRequest www = UnityWebRequest.Post(url, content);

        yield return(www.Send());

        string resultContent = string.Empty;

        if (!www.isNetworkError)
        {
            resultContent = www.downloadHandler.text;
        }
        Log.recv(resultContent);
        result(resultContent);
    }
        public static void GetAccessToken <T>(Action <T> cb)
        {
            var data = new YNoteRequestData();

            data.httpVerb = HTTPVerb.POST;
            data.url      = YNoteUtil.GetURL(OAUTH1_ACCESS_TOKEN);

            string http               = YNoteOAuthUtil.GetHttpVerbName(data.httpVerb);
            string callback           = OAUTH_CALLBACK;
            string method             = OAUTH_SHA1;
            string timeStamp          = YNoteOAuthUtil.GenerateTimeStampSec();
            string nonce              = YNoteOAuthUtil.GenerateNonce();
            string ver                = OAUTH_VER;
            string oauth_token        = YNoteUtil.oauth_token;
            string oauth_token_secret = YNoteUtil.oauth_token_secret;
            string oauth_verifier     = YNoteUtil.oauth_verifier;

            data.content = new Dictionary <string, string>();
            data.content.Add("oauth_consumer_key", YNoteUtil.consumerKey); // consumerKey
            data.content.Add("oauth_token", oauth_token);                  // 请求 request_token 时返回的 oauth_token
            data.content.Add("oauth_verifier", oauth_verifier);            // 授权码
            data.content.Add("oauth_signature_method", method);            // 签名方法
            data.content.Add("oauth_timestamp", timeStamp);                // 时间戳
            data.content.Add("oauth_nonce", nonce);                        // 随机串
            data.content.Add("oauth_version", ver);                        // oauth 版本

            string signature = YNoteOAuthUtil.GenerateOAuthSignature(http, data.url, data.content, YNoteUtil.consumerSecret, oauth_token_secret);

            data.content.Add("oauth_signature", signature); // 签名

            YNoteRequestManager.Instance.ParseRequest(data, cb);
        }
        public static void GetServerTime <T>(Action <T> cb)
        {
            var data = new YNoteRequestData();

            data.httpVerb = HTTPVerb.GET;
            data.url      = YNoteUtil.GetURL(OAUTH1_SERVER_TIME);
            YNoteRequestManager.Instance.ParseRequest(data, cb);
        }
        public static void UserLogin <T>(Action <T> cb)
        {
            var data = new YNoteRequestData();

            data.httpVerb    = HTTPVerb.GET;
            data.url         = string.Format("{0}?oauth_token={1}", YNoteUtil.GetURL(OAUTH1_USER_LOGIN), YNoteUtil.oauth_token);
            data.needOpenUrl = true;
            YNoteRequestManager.Instance.ParseRequest(data, cb);
        }
        public static void GetAllNotebook <T>(Action <T> cb)
        {
            var data = new YNoteRequestData();

            data.httpVerb = HTTPVerb.POST;
            data.url      = YNoteUtil.GetURL(API_ALL_NOTEBOOK);

            AppendOAuthContent(ref data);

            string http = YNoteOAuthUtil.GetHttpVerbName(data.httpVerb);
            string oauth_token_secret = YNoteUtil.access_token_secret;
            string signature          = YNoteOAuthUtil.GenerateOAuthSignature(http, data.url, data.content, YNoteUtil.consumerSecret, oauth_token_secret);

            data.content.Add("oauth_signature", signature); // 签名

            YNoteRequestManager.Instance.ParseRequest(data, cb);
        }
        public static void GetUserInfo <T>(Action <T> cb)
        {
            var data = new YNoteRequestData();

            data.httpVerb = HTTPVerb.GET;
            data.url      = YNoteUtil.GetURL(API_USER_INFO);

            AppendOAuthContent(ref data);

            string http = YNoteOAuthUtil.GetHttpVerbName(data.httpVerb);
            string oauth_token_secret = YNoteUtil.access_token_secret;
            string signature          = YNoteOAuthUtil.GenerateOAuthSignature(http, data.url, data.content, YNoteUtil.consumerSecret, oauth_token_secret);

            data.content.Add("oauth_signature", signature); // 签名
            data.url = YNoteOAuthUtil.GenerateQueryString(data.url, data.content);

            YNoteRequestManager.Instance.ParseRequest(data, cb);
        }
        public static void MoveNote <T>(string notePath, string targetNotebook, Action <T> cb)
        {
            var data = new YNoteRequestData();

            data.httpVerb = HTTPVerb.POST;
            data.url      = YNoteUtil.GetURL(API_MOVE_NOTE);

            AppendOAuthContent(ref data);
            data.content.Add("path", notePath);
            data.content.Add("notebook", targetNotebook);

            string http = YNoteOAuthUtil.GetHttpVerbName(data.httpVerb);
            string oauth_token_secret = YNoteUtil.access_token_secret;
            string signature          = YNoteOAuthUtil.GenerateOAuthSignature(http, data.url, data.content, YNoteUtil.consumerSecret, oauth_token_secret);

            data.content.Add("oauth_signature", signature); // 签名

            YNoteRequestManager.Instance.ParseRequest(data, cb);
        }
        public static void CreateNote <T>(string source, string author, string title, string content, Action <T> cb)
        {
            var data = new YNoteRequestData();

            data.httpVerb    = HTTPVerb.POST;
            data.url         = YNoteUtil.GetURL(API_NEW_NOTE);
            data.contentType = HTTPContentType.MULTIPART;

            AppendOAuthContent(ref data);

            string http = YNoteOAuthUtil.GetHttpVerbName(data.httpVerb);
            string oauth_token_secret = YNoteUtil.access_token_secret;
            string signature          = YNoteOAuthUtil.GenerateOAuthSignature(http, data.url, data.content, YNoteUtil.consumerSecret, oauth_token_secret);

            data.content.Add("oauth_signature", signature); // 签名

            data.content.Add("source", source);
            data.content.Add("author", author);
            data.content.Add("title", title);
            data.content.Add("content", content);

            YNoteRequestManager.Instance.ParseRequest(data, cb);
        }