谷歌Drive API交换授权代码有时会抛出500

时间:2012-08-09 09:38:06

标签: google-drive-api

使用此代码将来自Drive UI的请求的初始代码与可用于发出API请求的令牌进行交换。

public GoogleCredential exchangeCode(String authorizationCode) throws CodeExchangeException {
        try {
            GoogleTokenResponse response = new GoogleAuthorizationCodeTokenRequest(new NetHttpTransport(), new JacksonFactory(), CLIENT_ID, CLIENT_SECRET, authorizationCode, REDIRECT_URI).execute();
            return new GoogleCredential.Builder().setClientSecrets(CLIENT_ID, CLIENT_SECRET).setTransport(new NetHttpTransport()).setJsonFactory(new JacksonFactory()).build().setFromTokenResponse(response);
        }
        catch (Exception e) {
            log.error("An error occurred: " + e);
            throw new CodeExchangeException(null);
        }
    }

在大多数情况下,它有效,但在某些情况下(可能是5%),我得到

An error occurred: com.google.api.client.auth.oauth2.TokenResponseException: 500 Error processing OAuth 2 request
<HTML>
<HEAD>
<TITLE>Error processing OAuth 2 request</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Error processing OAuth 2 request</H1>
<H2>Error 500</H2>
</BODY>
</HTML>

可能是什么问题?

2 个答案:

答案 0 :(得分:0)

我得到'500内部服务器错误'到大约。 0.5%的API调用。我有一个重试机制,似乎总是在第二次尝试时成功。

    function doSomething(req) {
.
.
.
.
         restRequest.execute(function(resp) {
    var myResp = new Object();

    try {
        if (resp.error) {
            if (resp.error.code == "401") {
                do401(function() {
                    doSomething(req);
                }, req, myResp);
                return;
            }
            if (resp.error.code == "403") {
                cl("403 no auth " + resp.error.message);
                return;
            }
            if (resp.error.code == "500") {
                cl("server error 500, retrying " + resp.error.message);
                exponentialBackoff();
                                    doSomething(req);
                return;
            }
        }
    } catch (ex) {
        cl("[fg62] exception " + ex);
    }

答案 1 :(得分:0)

这是由授权过程中两台内部服务器之间的超时(OAuth 2.0服务器端流程的auth代码交换)引起的。我们在OAuth 2.0 Playground面临同样的问题。这应该已经由Google的auth团队解决了。 如果您再次遇到此问题,请告诉我们。

重试机制通常可以解决您从Drive API获得的瞬态503错误。但在这种情况下(来自Auth服务器的500个错误)它没有帮助,因为相同的呼叫每次都会为同一服务器配置中的相同用户超时(即远程地理区域中的用户和服务器)。