[教程][Blogger][C#] Blogger API v3 教程#6——OAuth 2.0 认证
12 Oct 2013【这个系列中..】
[教程][Blogger][C#] Blogger API v3教程#1 ——
前言
[教程][Blogger][C#] Blogger API v3 教程#2 —— 申请 Blogger API
的使用权限
[教程][Blogger][C#] Blogger API v3 教程#3 —— 获取API Key、Client ID
和 Client
Secret
[教程][Blogger][C#] Blogger API v3 教程#4 ——
设置开发环境、安装插件、添加Reference
[教程][Blogger][C#] Blogger API v3 教程#5 —— 获取Blog
Id
[教程][Blogger][C#] Blogger API v3 教程#6——OAuth 2.0
认证
[教程][Blogger][C#] Blogger API v3
教程#7——获取帖子列表
[教程][Blogger][C#] Blogger API v3
教程#8——更改帖子标题
至于OAuth 是啥,这里不做详细介绍
就是类似这样的东东:
1、先到这边获取Client Id 和 Client Secret :https://code.google.com/apis/console/b/0/?noredirect
2、创建两个string,储存Client ID 和 Secret
string clientID = "{CLIENT-ID}";
string clientSec = "{CLIENT-SECRET}";
3、Google Code Page 这里有个OAuth2.0 的 .net 版本例子,我们照搬改点东西就好了:
所以我们只要改一改我们的BloggerService 再加上一个function就行了:
先创建NativeApplicationClient
NativeApplicationClient provider = new NativeApplicationClient(GoogleAuthenticationServer.Description)
{
ClientIdentifier = clientID,
ClientSecret = clientSec
};
然后是增加一个函数
private static IAuthorizationState getAuth(NativeApplicationClient arg)
{
IAuthorizationState state = new AuthorizationState(new [] {BloggerService.Scopes.Blogger.GetStringValue()})
{
Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl)
};
Uri authUri = arg.RequestUserAuthorization(state);
Process.Start(authUri.ToString());
Console.WriteLine("Please enter auth code:");
string authCode = Console.ReadLine();
return arg.ProcessUserAuthorization(authCode, state);
}
创建OAuth2Authenticator
OAuth2Authenticator<NativeApplicationClient> auth = new OAuth2Authenticator<NativeApplicationClient>(provider, getAuth);
然后更改BloggerService
BloggerService blogService = new BloggerService(new BaseClientService.Initializer()
{
Authenticator = auth,
ApplicationName = "BloggerTest"
});
完整代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Apis.Blogger.v3;
using Google.Apis.Blogger.v3.Data;
using Google.Apis.Services;
using System.Diagnostics;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Util;
namespace BloggerTest
{
class Program
{
static void Main(string[] args)
{
string apiKey= "{API-KEY}";
string blogUrl= "{BLOG-URL}";
string clientID = "{CLIENT_ID}";
string clientSec = "{CLIENT_SECRET}";
NativeApplicationClient provider = new NativeApplicationClient(GoogleAuthenticationServer.Description)
{
ClientIdentifier = clientID,
ClientSecret = clientSec
};
OAuth2Authenticator<NativeApplicationClient> auth = new OAuth2Authenticator<NativeApplicationClient>(provider, getAuth);
BloggerService blogService = new BloggerService(new BaseClientService.Initializer()
{
Authenticator = auth,
ApplicationName = "BloggerTest"
});
BlogsResource.GetByUrlRequest getReq = blogService.Blogs.GetByUrl(blogUrl);
getReq.Key = apiKey;
Blog blog = getReq.Execute();
Console.WriteLine(blog.Id);
Console.ReadKey();
}
private static IAuthorizationState getAuth(NativeApplicationClient arg)
{
IAuthorizationState state = new AuthorizationState(new[] { BloggerService.Scopes.Blogger.GetStringValue() })
{
Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl)
};
Uri authUri = arg.RequestUserAuthorization(state);
Process.Start(authUri.ToString());
Console.WriteLine("Please enter auth code:");
string authCode = Console.ReadLine();
return arg.ProcessUserAuthorization(authCode, state);
}
}
}
编译运行后,程序会开启一个页面:
点击Accept
然后就有一长串字符,Copy 了 paste 进程序内 ([教程] 如何在CMD中Copy & Paste)
然后照常输出blog id
PS:Auth code 会过期的!
Published by Gary Ng