IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    WP8.1开发:新浪微博API调用示例

    编程小梦发表于 2014-11-01 13:18:16
    love 0

    之前分别给出了人人网在WP8.1RT和SL下的调用示例.新浪微博方法也基本一样.小梦今天给出大家在WP8.1RT中调用新浪微博API的具体源码.包含:

    • 新浪微博 OAuth2.0获取授权
    • 新浪微博获取用户基本信息
    • 新浪微博发表带图片微博
    • 新浪微博发表文字微博

    大家有兴趣的话可以依次为例去做一个新浪微博客户端.下面我直接上代码:

    WP8.1 新浪微博API 获取 code:

    private void WeiBoNavigate()//导航至微博登录
            {
                web.Navigate(new Uri(weiboUri, UriKind.Absolute));
    
                web.NavigationStarting += web_NavigationStarting;
            }
    
            void web_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
            {
                if (args.Uri.AbsoluteUri.ToString().Contains("code"))
                {
                    codeString = args.Uri.AbsoluteUri.ToString();
                    getCode(codeString);
                }
            }
    
            private void getCode(string codeString)
            {
                int i = 0, j = 0;
                i = codeString.IndexOf("code");
                j = codeString.Length - i - 5;
                string code= codeString.Substring(i + 5, j);
    
                HttpWeiboPost.HttpPost(key, secret, redirect_uri, code);
                Frame.Navigate(typeof(MainPage));
            }

    WP8.1 新浪微博API 获取 access token:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Windows.Data.Json;
    using Windows.UI.Popups;
    using Windows.Web.Http;
    
    namespace WeiBo
    {
        public class HttpWeiboPost
        {
            public static async void HttpPost(string client_id, string client_secret, string redirect_uri, string code)
            {
                try
                {
                    HttpClient httpClient = new HttpClient();
                    string posturi = "https://api.weibo.com/oauth2/access_token?";
                    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri(posturi));
                    HttpFormUrlEncodedContent postData = new HttpFormUrlEncodedContent(
                        new List>
                        {
                            new KeyValuePair("client_id", client_id),
                            new KeyValuePair("client_secret", client_secret),
                            new KeyValuePair("grant_type","authorization_code"),
                            new KeyValuePair("redirect_uri", redirect_uri),
                            new KeyValuePair("code",code),
    
                        }
                    );
                    request.Content = postData;
                    HttpResponseMessage response = await httpClient.SendRequestAsync(request);
                    string responseString = await response.Content.ReadAsStringAsync();
                    JsonObject token = JsonObject.Parse(responseString);
                    string access_token = token.GetNamedString("access_token").ToString();
                    App.Access_token=access_token;
                    App.appSettings.Values["access_token"] = access_token;
    
                }
                catch (Exception ex)
                {
                    Msg(ex.Message.ToString());
                }
            }
            private static async void Msg(string msg)
            {
                await new MessageDialog(msg).ShowAsync();
            }
        }
    }

    WP8.1 新浪微博API 获取 用户的UID

    public  class HttpWeiboGet
        {
    
            private static string responseString;
    
            public static async void HttpGet(string uri)
            {
                HttpClient httpClient = new HttpClient();
    
                //var headers = httpClient.DefaultRequestHeaders;
                //headers.UserAgent.ParseAdd("ie");
                //headers.UserAgent.ParseAdd("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
                try
                {
                    HttpResponseMessage response = new HttpResponseMessage();
                    response = await httpClient.GetAsync(new Uri(uri, UriKind.Absolute));
    
                    response.EnsureSuccessStatusCode();
    
                    responseString = await response.Content.ReadAsStringAsync();
    
                    JsonObject token = JsonObject.Parse(responseString);
                    string   UID = token.GetNamedNumber("uid").ToString();
                    App.appSettings.Values["userID"] = UID;
                }
                catch (Exception ex)
                {
                    Msg("网络请求失败!" + ex.Message.ToString());
                }
            }
    
            private static async void Msg(string msg)
            {
                await new MessageDialog(msg).ShowAsync();
            }
        }

    WP8.1 新浪微博API 获取 用户信息:

    public class HttpWeiboGetUseInfo
        {
            private static string responseString;
    
            public static async void HttpGet(string uri)
            {
                HttpClient httpClient = new HttpClient();
    
                //var headers = httpClient.DefaultRequestHeaders;
                //headers.UserAgent.ParseAdd("ie");
                //headers.UserAgent.ParseAdd("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
                try
                {
                    HttpResponseMessage response = new HttpResponseMessage();
                    response = await httpClient.GetAsync(new Uri(uri, UriKind.Absolute));
    
                    response.EnsureSuccessStatusCode();
    
                    responseString = await response.Content.ReadAsStringAsync();
    
                    JsonObject user = JsonObject.Parse(responseString);
                    string UID = user.GetNamedNumber("id").ToString();
                    string name = user.GetNamedString("name").ToString();
                    string description = user.GetNamedString("description").ToString();
                }
                catch (Exception ex)
                {
                    Msg("网络请求失败!" + ex.Message.ToString());
                }
            }
    
            private static async void Msg(string msg)
            {
                await new MessageDialog(msg).ShowAsync();
            }
        }

    WP8.1 新浪微博API 发表文字微博:

    public  class HttpWeiboPostNew
        {
            public static async void HttpPost(string postMsg)
            {
                try
                {
                    HttpClient httpClient = new HttpClient();
                    string posturi = "https://api.weibo.com/2/statuses/update.json?";
                    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri(posturi));
                    HttpFormUrlEncodedContent postData = new HttpFormUrlEncodedContent(
                        new List>
                        {
                            new KeyValuePair("access_token",App.Access_token),
                            new KeyValuePair("status", postMsg),
    
                        }
                    );
                    request.Content = postData;
                    HttpResponseMessage response = await httpClient.SendRequestAsync(request);
                    string responseString = await response.Content.ReadAsStringAsync();
    
                }
                catch (Exception ex)
                {
                    Msg(ex.Message.ToString());
                }
            }
            private static async void Msg(string msg)
            {
                await new MessageDialog(msg).ShowAsync();
            }
        }

    WP8.1 新浪微博API 发表图片微博:

    public class HttpWeiboImagePost
        {
            public static async void HttpImagePost(string status, string pic )
            {
                HttpClient httpClient = new HttpClient();
                try
                {
                    string posturi = "https://upload.api.weibo.com/2/statuses/upload.json?access_token=" + App.Access_token;
                    StorageFile file1 = await StorageFile.GetFileFromApplicationUriAsync(new Uri(pic, UriKind.Absolute));
                    IRandomAccessStreamWithContentType stream1 = await file1.OpenReadAsync();
                    HttpStreamContent streamContent1 = new HttpStreamContent(stream1);
                    HttpStringContent stringContent = new HttpStringContent(status);
                    HttpMultipartFormDataContent fileContent = new HttpMultipartFormDataContent();
                    fileContent.Add(stringContent, "status");
                    fileContent.Add(streamContent1, "pic", "pic.jpg");
                    HttpResponseMessage response = await httpClient.PostAsync(new Uri(posturi), fileContent);
                    string responString = await response.Content.ReadAsStringAsync();
    
                }
                catch (Exception ex)
                {
                    Msg(ex.Message.ToString());
                }
    
            }
    
            private static async void Msg(string msg)
            {
                await new MessageDialog(msg).ShowAsync();
            }
        }

     如果小梦的文章对你有帮助!欢迎支持小梦!



沪ICP备19023445号-2号
友情链接