2

gwt-plus-v1-0.2-alpha API を使用して次のことを行います。

  1. Google ログインを許可する
  2. サインインしているユーザーの情報を取得する

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
            }
        });
    }
}
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 に答える