JDCB 新浪微博开放平台SDK for WinRT

初版发布,相信还有不少Bug,希望大家多提意见。谢谢!

这是啥?

新浪微博开放平台API for WinRT 是一个基于新浪微博开放平台第二版API接口进行封装的一个针对Windows 8 平台(Xaml 应用)的软件开发包。本SDK有助于.Net平台开发人员在 Windows 8 移动平台上快速简便地开发微博相关应用。

 

登录的示例代码:

public class LoginViewModel : SinaWeibo_Win8AppStore.Common.BindableBase
{
    private const string AppKey = "967389259";
    private const string AppSecret = "001379b11292669a5399131cfc5b806d";
    private const string Callback = "http://JDCB.com";

    public async Task<WeiboClientV2> LoginAsync()
    {
        var weibo = new SinaWeiboClientV2.WeiboClientV2();
        weibo.Initialize(AppKey, AppSecret, Callback);

        var json = Windows.Storage.ApplicationData.Current.LocalSettings.Values["AccessToken"] as string;
        if (!string.IsNullOrEmpty(json)) 
        {
            weibo.AccessToken = await JsonConvert.DeserializeObjectAsync<OAuthAccessToken>(json);
        }
        
        if (weibo.AccessToken == null)
        {
            // 认证
            await weibo.AuthenticateAsync();
        }
        // 授权
        await weibo.AuthorizeAccessAsync(System.Threading.CancellationToken.None);
        Windows.Storage.ApplicationData.Current.LocalSettings.Values["AccessToken"] = 
            await JsonConvert.SerializeObjectAsync(weibo.CurrentAuthToken.ContextData["AccessToken"]);

        return weibo;
    }
}
采用 WinRT 的 WebAuthenticationBroker 进行应用授权。
(下载示例的童鞋们,别忘记在自己的应用中加上测试账号,否则临时应用的授权不会通过)


发布微博的示例代码:
var request = new SinaWeiboClientV2.StatusesUpdate.Request { Status = this.InputArea.Text };
var response = await Weibo.ExecuteStatusesUpdateAsync(request);
上传图片的示例代码:
var fileInfo = new RestAPICore.FileUploadInfo();
fileInfo.LocalPath = ImageFile.Name;
fileInfo.StreamFactory = GetImageFile(ImageFile);
var request = new SinaWeiboClientV2.StatusesUpload.Request
{
    Status = this.InputArea.Text,
    Pic = fileInfo
};
var response = await Weibo.ExecuteStatusesUploadAsync(request);

private async Task<Stream> GetImageFile(StorageFile file)
{
    var s = await file.OpenAsync(FileAccessMode.Read);
    return s.AsStream();
}
读取最新关注微博的示例代码:
 var response = await Weibo.ExecuteStatusesFriendsTimelineAsync(request);
 if (string.IsNullOrEmpty(_maxId))
 {
     foreach (var item in response.Value.Statuses)
     {
         _statusFriendsTimeline.Add(item);
     }
 }
 

附,如何设置测试账号:
 

Last edited Jan 8, 2013 at 6:11 AM by FelixFang, version 7

Comments

No comments yet.