Google OAuth api

Google OAuth api

我正在尝试通过 API 访问服务器。可以不打开浏览器访问,如提供的示例

curl "token url" -d "grant_type=password&client_id=&client_secret=&username=&password="

所以我正在尝试使用 Google 的 API,特别是 PasswordTokenRequest,基本上是从 javadoc 复制代码 我通过 maven 下载了 1.20.0 库,每个方法和 class 都被导入,但 setRedirectUri() 没有。 IntelliJ IDEA 一直告诉我无法解析这个方法,尽管它在官方文档中使用过。那么javadoc中提供的示例是错误的吗?如果是这样,如果 API 需要 Bearer 令牌并且 POST 主体看起来像这样,那么访问 API 的正确方法是什么?

curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" --header "Authorization: Bearer 859ea2450228a5d655e3a8c2f9a5aedc30b591df" --header "Accept-Language: en" -d "{
  \"jsonrpc\": \"2.0\",
  \"method\": \"getByID\",
  \"params\": {
    \"Id\": 1
  },
  \"id\": 1
}" "rpc url"

事实证明,javadoc 中提供的代码确实错误。当我这样改变它时,它起作用了:

      TokenResponse response =
      new PasswordTokenRequest(new NetHttpTransport(), new JacksonFactory(),
          new GenericUrl("https://server.example.com/token"), client_id, secret)
          .setClientAuthentication(
              new BasicAuthentication(username, password)).execute();