2

我正在使用 gwt-plus-v1-0.2-alpha API 来:

  1. 允许谷歌登录
  2. 获取已登录用户的信息

谷歌登录有效,但获取用户信息失败

Cannot call method 'newHttpRequest' of undefined

错误。

以下是我的GoogleApi助手类:

public final class GoogleApi {
    private static final Plus plus = GWT.create(Plus.class);

    private final String clientId;

    private final ClientOAuth2Login oAuth2Login;
    private ClientGoogleApiRequestTransport requestTransport;

    /**
     * @param clientId
     *            This app's personal client ID assigned by the Google APIs
     *            Console (http://code.google.com/apis/console)
     */
    public GoogleApi(EventBus eventBus, String clientId) {
        this.clientId = clientId;
        requestTransport = new ClientGoogleApiRequestTransport();
        requestTransport.setApplicationName(MY_APP_NAME)
                .setApiAccessKey(MY_API_KEY);
        plus.initialize(eventBus, requestTransport);
        oAuth2Login = new ClientOAuth2Login(clientId);
        oAuth2Login.withScopes(PlusAuthScope.PLUS_ME);
    }

    public void login(final Receiver<String> callback) {
        oAuth2Login.login(new Receiver<String>() {
            @Override
            public void onSuccess(String response) {
                requestTransport.setAccessToken(response);
                callback.onSuccess(response);
            }

            @Override
            public void onFailure(ServerFailure error) {
                Window.alert(error.getMessage());
            }
        });
    }

    public void getUserInfo(Receiver<Person> receiver) {
        plus.people().get("me").to(receiver).fire();
    }
}

下面显示了发生故障的位置:

GoogleApi googleApi = new GoogleApi(eventBus, MY_CLIENT_ID);

googleApi.login(new Receiver<String>() {
    @Override
    public void onSuccess(final String token) {
        // login is successful and access token is received

        // but the following call fails with "Cannot call method 'newHttpRequest'
        // of undefined" error
        googleApi.getUserInfo(new Receiver<Person>() {
            @Override
            public void onSuccess(Person person) {
                // never gets here
            }

            @Override
            public void onFailure(ServerFailure error) {
                // nor here
            }
        });
    }
}
4

2 回答 2

3

尝试更新您的 gwt-google-apis 库,早在 6 月份就有一个更改,将调用替换为$wnd.googleapis.newHttpRequest()调用$wnd.gapi.client.rpcRequest()https ://code.google.com/p/gwt-google-apis/source/detail?r=2041

身份验证起作用的原因是它不使用相同的代码与服务器对话。

于 2012-11-26T09:40:17.907 回答
1

我玩过代码(仍然使用 1.0.2-alpha 版)并让它工作!以下是更新后的代码:

public final class GoogleApi {
    private static final Plus plus = GWT.create(Plus.class);

    private final String clientId;

    private EventBus eventBus;

    private final ClientOAuth2Login oAuth2Login;
    private ClientGoogleApiRequestTransport requestTransport;

    private String accessToken;

    /**
     * @param clientId
     *            This app's personal client ID assigned by the Google APIs
     *            Console (http://code.google.com/apis/console)
     */
    public GoogleApi(final EventBus eventBus, String clientId) {
        this.eventBus = eventBus;
        this.clientId = clientId;
        oAuth2Login = new ClientOAuth2Login(clientId);
        oAuth2Login.withScopes(PlusAuthScope.PLUS_ME);
    }

    public void login(final Receiver<String> callback) {
        oAuth2Login.login(new Receiver<String>() {
            @Override
            public void onSuccess(String response) {
                accessToken = response;
                callback.onSuccess(response);
            }

            @Override
            public void onFailure(ServerFailure error) {
                Window.alert(error.getMessage());
            }
        });
    }

    public void getUserInfo(final Receiver<Person> receiver) {
        requestTransport = new ClientGoogleApiRequestTransport();
        requestTransport.setApplicationName(ClientConstants.GOOGLE_APP_NAME)
                .setApiAccessKey(ClientConstants.GOOGLE_API_KEY)
                .setAccessToken(accessToken);
        requestTransport.create(new Receiver<GoogleApiRequestTransport>() {
            @Override
            public void onSuccess(GoogleApiRequestTransport transport) {
                plus.initialize(eventBus, transport);
                plus.people().get("me").to(receiver).fire();
            }
        });
    }
}

不过,我不知道这有多有效。每次调用 getUserInfo() 时都会调用 Plus 的 initialize() 方法。

我的下一步是使用最新版本的 API 手动构建一个 jar 并相应地调整我的代码:/ 祝我好运!

于 2012-11-26T12:46:20.513 回答