如何使用 google-oauth-java-client

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14393811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:08:03  来源:igfitidea点击:

how to use google-oauth-java-client

javagoogle-oauth

提问by karl li

I want to use google-oauth-java-clientto get authorization codefrom sina weibo

我想用google-oauth-java-client新浪微博获取授权码

this is the GET method that get code from sina

这是从新浪获取代码的GET方法

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?

有人可以给我一些建议吗?

采纳答案by karl li

sI'm sorry that I make a mistake! Get method use browser and return the code Post method use HttpRequest and we can get parameter from HtppResponse

对不起,我犯了一个错误!Get方法使用浏览器返回代码Post方法使用HttpRequest,我们可以从HtppResponse中获取参数

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

因此,如果您想获取代码,只需使用浏览器并重定向到 url 即可获取代码

Here is how I get access_token

这是我获取 access_token 的方法

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

如果你愿意,你可以使用 google-oauth-java-client 来授权 twitter facebook

I solve this by javadoc which show me some examples Thisis the root of JavaDoc Thisis the package I use to solve Here is the example I write

我通过javadoc解决了这个问题,它向我展示了一些例子 是JavaDoc的根 是我用来解决的包这是我写的例子

//   https://server.example.com/token server url example
try {
  TokenResponse response =
      new AuthorizationCodeTokenRequest(new NetHttpTransport(), new HymansonFactory(),
          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());
  }
}

回答by TechSpellBound

Thisand thiswill help you. First understand the mechanism and implement it according to your scenario.

会帮助你。先了解一下机制,根据你的场景去实现。