【问题标题】:How to get authorization code for GoogleAuthorizationCodeTokenRequest in Java?如何在 Java 中获取 GoogleAuthorizationCodeTokenRequest 的授权码?
【发布时间】:2012-11-07 04:15:54
【问题描述】:
        HttpTransport netTransport = new NetHttpTransport();
        JsonFactory jsonFactory = new JacksonFactory();
        GoogleTokenResponse token;
        try {
            token = new GoogleAuthorizationCodeTokenRequest(
                    netTransport,
                    jsonFactory,
                    CLIENT_ID,
                    CLIENT_SECRET,
                    CODE,
                    REDIRECT).execute();

            GoogleCredential cd = new GoogleCredential().setAccessToken(token
                    .getAccessToken());

            Plus plus = Plus
                    .builder(new NetHttpTransport(), new JacksonFactory())
                    .setApplicationName(APP_NAME)
                        .setHttpRequestInitializer(cd).build();

            Person profile = plus.people().get("me").execute();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

大家好,我正在尝试在我的 Google App Engine 应用程序中从 Google+ 获取一些用户信息,但我不知道要放置什么/如何获取 GoogleAuthorizationCodeTokenRequest 的 CODE 参数。任何帮助是极大的赞赏。谢谢。

【问题讨论】:

    标签: java token google-plus-signin


    【解决方案1】:

    您可以从重定向 URL 中获取 de CODE。

    HttpTransport httpTransport = new NetHttpTransport();
                    JsonFactory jsonFactory = new JacksonFactory();
            
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET,
            Arrays.asList(DriveScopes.DRIVE)).setAccessType("offline").setApprovalPrompt("auto").build();
    String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build();
    res.sendRedirect(url);
    

    【讨论】:

    • 什么是 REDIRECT_URI?
    【解决方案2】:

    如果你想从google api获取代码

    List<String> responsetype = Arrays.asList("code");
    
    // Step 1: Authorize -->
    String authorizationUrl = new GoogleBrowserClientRequestUrl(clientId, redirectUrl, Arrays.asList(scope))
                .setResponseTypes(responsetype)
                .build();
    
    // Point or redirect your user to the authorizationUrl.
    System.out.println("Go to the following link in your browser:");
    System.out.println(authorizationUrl);
    

    它会返回一个从浏览器点击 url 的 Url,你会在响应中得到一个代码。

    【讨论】:

      【解决方案3】:

      我会尝试改进 Remy Ticona 的答案。 基本的 oAuth 授权流程是:

      1. 请求用户访问。您的应用应将用户重定向到授权网址。在该页面上,Google 要求用户访问。
      2. 在用户授予访问权限后,他将被重定向到 REDIRECT_URL。在那里您可以选择最适合您的选项:
        1. 您的应用程序具有内置的网络浏览器。使用urn:ietf:wg:oauth:2.0:oob:auto 作为网址。当用户授予访问权限时,您的应用应从网页标题中读取访问代码。
        2. 您的应用程序作为网络服务器。使用您的网络应用程序的网址。访问代码将是一个查询参数,您可以处理它
        3. 您的应用不具备上述功能。使用 urn:ietf:wg:oauth:2.0:oob 作为 REDIRECT_URL。当用户授予访问权限时,他将被重定向到包含访问代码的页面,并请求将其复制并粘贴到您的应用中。

      您可以在这里找到详细信息:https://developers.google.com/identity/protocols/OAuth2InstalledApp?hl=RU#choosingredirecturi

      小例子:

      GoogleAuthorizationCodeFlow authorizationCodeFlow = new GoogleAuthorizationCodeFlow
                          .Builder(httpTransport, jsonFactory, clientId, clientSecret, scopes)
                          .setCredentialDataStore(new MemoryDataStoreFactory().getDataStore("tokens"))
                          .build();
      String userId = "user-id";
      Credential credential = authorizationCodeFlow.loadCredential(userId);
      if (credential == null) {
              GoogleAuthorizationCodeRequestUrl authorizationUrl = authorizationCodeFlow.newAuthorizationUrl();
              authorizationUrl.setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI);
              LOGGER.error("Please, authorize application. Visit {}", authorizationUrl);
              Scanner s = new Scanner(System.in);
              String code = s.nextLine();
              GoogleAuthorizationCodeTokenRequest tokenRequest = authorizationCodeFlow.newTokenRequest(code);
              tokenRequest.setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI);
              GoogleTokenResponse tokenResponse = tokenRequest.execute();
              credential = authorizationCodeFlow.createAndStoreCredential(tokenResponse, userId);
          }
      

      【讨论】:

        猜你喜欢
        • 2020-02-26
        • 1970-01-01
        • 2015-08-08
        • 2017-08-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-12
        • 2020-07-01
        • 2020-06-09
        相关资源
        最近更新 更多