我使用 gwt-plus-v1-0.2-alpha API 来:
- 允许 Google 登录
- 获取登录用户的信息
Google 登录正常,但获取用户信息失败,并显示
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
}
});
}
}
最佳答案
尝试更新您的 gwt-google-apis 库,六月份发生了一项更改,替换了对 $wnd.googleapis.newHttpRequest()
的调用调用$wnd.gapi.client.rpcRequest()
: https://code.google.com/p/gwt-google-apis/source/detail?r=2041
身份验证起作用的原因是它不使用相同的代码与服务器通信。
关于java - gwt-google-apis 调用导致 "Cannot call method ' newHttpRequest' 未定义”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13556641/