Note: I got a lot of inspiration from this gem. Thanks simsicon.
CSDN-SDK is a Ruby gem that provides a wrapper for interacting with CSDN API, which is currently the latest version. Check out the link if you are interested in browsing the details. The output data format is Hashie::Mash, check out here. If you are not familiar with oauth2, I recommend that you read this article.
- csdn account
- csdn app API key
- csdn app API secret
$ gem install csdn-sdkThe example have been moved to here.
-
How to get the token?
OAuth2 is simpler than its first version. In order to get the access token, you first need to get an Authorization Code through a callback request. Now use the following code to get the token.
CsdnOAuth2::Config.api_key = YOUR_KEY CsdnOAuth2::Config.api_secret = YOUR_SECRET CsdnOAuth2::Config.redirect_uri = YOUR_CALLBACK_URL
If you are developing in your localhost, you can set YOUR_CALLBACK_URL as 'http://127.0.0.1/callback' something. Then set your csdn app account's callback URL as this URL too. Csdn will call the URL using GET method, which will then enable you to retrieve the authorization code.
client = CsdnOAuth2::Client.new
Or you can pass the key and secret to new a client if you did not set CsdnOAuth2::Config
Redirect to this URL. If user grants you permission, then you will get the authorization code.
client.authorize_url
In your callback handling method, you should add something like the following,
client.auth_code.get_token(params[:code])
which will give permission to your client.
-
How to get a blog info and reply it? (or call other interfaces)
get a blog's info and reply it
client.blog.get_article(params[:article_id]) client.blog.postcomment(params[:article_id], params[:content])
Simply get a user's info.
client.account.get_info