【问题标题】:Examples for oauth1 using google-api-java-oauth [closed]使用 google-api-java-oauth 的 oauth1 示例 [关闭]
【发布时间】:2013-02-18 02:32:16
【问题描述】:

我一直在寻找使用 google oauth java 包进行身份验证的示例: https://code.google.com/p/google-oauth-java-client/

我已经设法找到使用此包的 oauth2 身份验证示例,但我找不到任何用于 oauth1 的示例。该文档简要概述了“典型应用程序流程”,但忽略了所有细节。

有人对我在哪里可以找到使用 thing 包的 oauth1 身份验证示例有任何建议吗?

【问题讨论】:

    标签: java api oauth


    【解决方案1】:

    基于来自Google's OAuth 1.0 guideRFC 5849google-oauth-java-client JavaDoc,示例应如下所示:

        OAuthHmacSigner signer = new OAuthHmacSigner();
        // Get Temporary Token
        OAuthGetTemporaryToken getTemporaryToken = new OAuthGetTemporaryToken(TOKEN_SERVER_URL);
        signer.clientSharedSecret = OAuth2ClientCredentials.CONSUMER_SECRET;
        getTemporaryToken.signer = signer;
        getTemporaryToken.consumerKey = OAuth2ClientCredentials.CONSUMER_KEY;
        getTemporaryToken.transport = new NetHttpTransport();
        OAuthCredentialsResponse temporaryTokenResponse = getTemporaryToken.execute();
    
        // Build Authenticate URL
        OAuthAuthorizeTemporaryTokenUrl accessTempToken = new OAuthAuthorizeTemporaryTokenUrl(AUTHENTICATE_URL);
        accessTempToken.temporaryToken = temporaryTokenResponse.token;
        String authUrl = accessTempToken.build();
    
        // Redirect to Authenticate URL in order to get Verifier Code
        System.out.println(authUrl);
        
        // Get Access Token using Temporary token and Verifier Code
        OAuthGetAccessToken getAccessToken = new OAuthGetAccessToken(ACCESS_TOKEN_URL);
        getAccessToken.signer = signer;
        getAccessToken.temporaryToken=temporaryTokenResponse.token;
        getAccessToken.transport = new NetHttpTransport();
        getAccessToken.verifier= "VERIFIER_CODE";
        getAccessToken.consumerKey = OAuth2ClientCredentials.CONSUMER_KEY;
        OAuthCredentialsResponse accessTokenResponse = getAccessToken.execute();
    
        // Build OAuthParameters in order to use them while accessing the resource
        OAuthParameters oauthParameters = new OAuthParameters();
        signer.tokenSharedSecret = accessTokenResponse.tokenSecret;
        oauthParameters.signer = signer;
        oauthParameters.consumerKey = OAuth2ClientCredentials.CONSUMER_KEY;
        oauthParameters.token = accessTokenResponse.token;
        oauthParameters.verifier = "VERIFIER_CODE";
    
        // Use OAuthParameters to access the desired Resource URL
        HttpRequestFactory requestFactory = new NetHttpTransport().createRequestFactory(oauthParameters);
        GenericUrl genericUrl = new GenericUrl("RESOURCE_URL");
        HttpResponse response = requestFactory.buildGetRequest(genericUrl).execute();
        System.out.println(response.parseAsString());
    

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      上面的例子非常有用。

      关于将此库与非标准 oAuth 1.0 实现结合使用的说明。我使用的是Goodreads oAuth API,这似乎是oAuth Bible 所称的“失败的OAuth 1.0a 3-Legged 实现”,这意味着在将授权用户重定向回您的用户后,它不会发回验证码回调地址。在这种情况下,您需要删除上面引用 VERIFIER_CODE 的所有行并添加:

      signer.tokenSharedSecret = temporaryTokenResponse.tokenSecret;
      

      行前:

      OAuthCredentialsResponse accessTokenResponse = getAccessToken.execute();
      

      我花了一段时间才弄清楚,所以希望能帮助别人。

      【讨论】:

        猜你喜欢
        • 2011-01-17
        • 1970-01-01
        • 2012-10-27
        • 1970-01-01
        • 2011-06-20
        • 1970-01-01
        • 2012-12-04
        • 1970-01-01
        • 2023-02-23
        相关资源
        最近更新 更多