[Oauth authentication] Use scribe to implement oauth

Source: Internet
Author: User
Tags oauth

Scribe here refers to the oauth library, the Project address: https://github.com/fernandezpablo85/scribe-java, not the Facebook one (https://github.com/facebook/scribe ).

Oauth1.0a \ oauth2.0 is supported. I personally feel very good.

The usage is actually very simple. You can check the official website. The Sina Weibo request instance is attached here:

package com.yfz;import java.util.*;import org.scribe.builder.*;import org.scribe.builder.api.*;import org.scribe.model.*;import org.scribe.oauth.*;public class SinaWeiboExample{private static final String NETWORK_NAME = "SinaWeibo";private static final String PROTECTED_RESOURCE_URL = "http://api.t.sina.com.cn/account/verify_credentials.json";private static final String API_KEY = "2178327133";private static final String API_SECRET = "67b0153c234c5423bcbba7078af6d127";public static void main(String[] args){// Replace these with your own api key and secretString apiKey = "your key";String apiSecret = "your secret";OAuthService service = new ServiceBuilder()                        .provider(SinaWeiboApi.class)                        .apiKey(apiKey)                        .apiSecret(apiSecret)                        .build();Scanner in = new Scanner(System.in);System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");System.out.println();// Grab a request token.System.out.println("Fetching request token.");Token requestToken = service.getRequestToken();System.out.println("Got it ... ");System.out.println(requestToken.getToken());// Obtain the Authorization URLSystem.out.println("Fetching the Authorization URL...");String authorizationUrl = service.getAuthorizationUrl(requestToken);System.out.println("Got the Authorization URL!");System.out.println("Now go and authorize Scribe here:");System.out.println(authorizationUrl);System.out.println("And paste the authorization code here");System.out.print(">>");Verifier verifier = new Verifier(in.nextLine());System.out.println();// Trade the Request Token and Verfier for the Access TokenSystem.out.println("Trading the Request Token for an Access Token...");Token accessToken = service.getAccessToken(requestToken, verifier);System.out.println("Got the Access Token!");System.out.println("(if your curious it looks like this: "+ accessToken + " )");System.out.println();// Now let's go and ask for a protected resource!System.out.println("Now we're going to access a protected resource...");OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);service.signRequest(accessToken, request);Response response = request.send();System.out.println("Got it! Lets see what we found...");System.out.println();System.out.println(response.getCode());System.out.println(response.getBody());System.out.println();System.out.println("Thats it man! Go and build something awesome with Scribe! :)");}}

If you have any questions, leave a message.

Attach the oauth instance of Sina Weibo, which is relatively simple. In this example, you will first perform authentication, then obtain user information, and then release the new thin image. (Check the log !)

Example: http://download.csdn.net/detail/feng88724/3671033

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.