Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use google-oauth-java-client?

I want to use google-oauth-java-client to get authorization code from Sina Weibo.

This is the GET method that get code from Sina

https://api.weibo.com/oauth2/authorize?client_id=70090552&response_type=code&redirect_uri=http://127.0.0.1/weibo

Please solve this without web page, only client!

Can anybody give me some advise?

like image 913
karl li Avatar asked Jan 18 '13 06:01

karl li


People also ask

How does OAuth work in Java?

One of OAuth's key patterns is a resource server. A resource server accepts an access token. If the token is valid, it gives a client access to the resource owner's data. In this example, a client is an app, the resource owner is a user, and the resource server is the Java API you develop.


2 Answers

Get method use browser and return the code Post method use HttpRequest and we can get parameter from HtppResponse

So if you want to get code, just use browser and redirect to the url to get code

Here is how I get access_token

If you want, you can use google-oauth-java-client to authorization twitter facebook

I solve this by javadoc which show me some examples. This is the root of JavaDoc and this is the package I use to solve

Here is the example I write

//   https://server.example.com/token server url example
try {
  TokenResponse response =
      new AuthorizationCodeTokenRequest(new NetHttpTransport(), new JacksonFactory(),
          new GenericUrl("here is the server url "), "here write your code")
          .setRedirectUri("here write the redirectUrl")
          .set("client_id","here write your client_id")
          .set("client_secret","here write your client_secret")
          .set("Other else need","Other else need")
          .execute();
  System.out.println("Access token: " + response.getAccessToken());
} catch (TokenResponseException e) {
  if (e.getDetails() != null) {
    System.err.println("Error: " + e.getDetails().getError());
    if (e.getDetails().getErrorDescription() != null) {
      System.err.println(e.getDetails().getErrorDescription());
    }
    if (e.getDetails().getErrorUri() != null) {
      System.err.println(e.getDetails().getErrorUri());
    }
  } else {
    System.err.println(e.getMessage());
  }
}
like image 86
karl li Avatar answered Nov 13 '22 08:11

karl li


Here you can get find the solution.

http://code.google.com/p/google-oauth-java-client/source/browse/dailymotion-cmdline-sample/src/main/java/com/google/api/services/samples/dailymotion/cmdline/DailyMotionSample.java?repo=samples

like image 2
SANN3 Avatar answered Nov 13 '22 07:11

SANN3