Prafulljohri / gwt-google-apis

Automatically exported from code.google.com/p/gwt-google-apis
0 stars 0 forks source link

gwt-gadgets feature request: add oauth support to RPC classes #465

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Found in Release:
gwt-gadgets 1.2.0

Detailed description:
A great example of using OAuth from gadgets is provided in the gwt-examples 
library:
http://code.google.com/p/gwt-examples/source/browse/#svn%2Ftrunk%2FDemoGwtOAuthP
opUp

In order to implement this in gadgets requires a non-trivial amount of 
extending the functionality of the RPC classes for gadgets.  Rolling this 
functionality into the respective classes natively in the gwt-gadgets library 
would be very useful.

Workaround if you have one:

1. Extend RequestOptions with additional Oauth params:
public class OAuthGadgetRequestOptions extends RequestOptions {

  protected OAuthGadgetRequestOptions() {
  }

  public static OAuthGadgetRequestOptions newInstanceEx() {
    return JavaScriptObject.createObject().cast();
  }

  public final native RequestOptions setOAuthServiceName(String s) /*-{
    this.OAUTH_SERVICE_NAME = s;
    return this;
  }-*/;

  public final native RequestOptions setOAuthUseToken(String s) /*-{
    this.OAUTH_USE_TOKEN = s;
    return this;
  }-*/;

}

2. Create new Exception for Oauth exception:
public class OAuthException extends RuntimeException {

    private String OAuthUrl;

    public OAuthException(String oAuthUrl) {
        super();
        OAuthUrl = oAuthUrl;
    }

    public String getOAuthUrl() {
        return OAuthUrl;
    }

    public void setOAuthUrl(String oAuthUrl) {
        OAuthUrl = oAuthUrl;
    }

}

3. Extend GadgetsRequest with new fireOnResponseReceived method:
...
    void fireOnResponseReceived(final ResponseReceivedEvent<Object> event, RequestCallback callback) {
        cancelTimer();
        if (!canceled) {
            isPending = false;
            com.google.gwt.gadgets.client.io.Response<Object> response = event.getResponse();

            if (response.getStatusCode() == 200 && response.getOauthApprovalUrl() != null) {
                callback.onError(this, new OAuthException(response.getOauthApprovalUrl()));
            } else if (response.getStatusCode() == 200 && response.getOauthApprovalUrl() == null) {
                callback.onResponseReceived(this, createResponse(event));
            } else {
                String errorMessage = event.getResponse().getText() + " Errors:" + event.getResponse().getErrors();
                callback.onError(this, new RuntimeException(errorMessage));
            }
        } 
    }
...

4. Extend RequestBuilder with custom doSend method:
...
  private OAuthGadgetsRequest doSend(String requestData,
      final RequestCallback callback) {
      OAuthGadgetRequestOptions options = OAuthGadgetRequestOptions.newInstanceEx();
        options.setMethodType(MethodType.POST);
        options.setPostData(requestData);
        options.setAuthorizationType(AuthorizationType.OAUTH);
        options.setOAuthServiceName("oauthService");
        options.setOAuthUseToken("always");
        options.setHeader("Content-Type", "text/x-gwt-rpc; charset=UTF-8");

    final OAuthGadgetsRequest gadgetsRequest = new OAuthGadgetsRequest(
        getTimeoutMillis(), callback);
    gadgetsRequest.setPending(true);

    IoProvider.get().makeRequest(getUrl(),
        new ResponseReceivedHandler<Object>() {
          public void onResponseReceived(ResponseReceivedEvent<Object> event) {
            gadgetsRequest.fireOnResponseReceived(event, callback);
          }
        }, options);

    return gadgetsRequest;
  }
...

Links to the relevant GWT Developer Forum posts:

Original issue reported on code.google.com by cw...@cloudsherpas.com on 2 May 2011 at 2:32

GoogleCodeExporter commented 9 years ago
Just to make sure I understand this, is there anything other than extending the 
GadgetsRequest class that is specific to using the Gadgets API?   

I guess I'm just wondering whether the Gadgets API is the right place for this 
to live.

Original comment by zundel@google.com on 14 May 2011 at 1:33

GoogleCodeExporter commented 9 years ago
Agreed.  Better oauth support would be useful in the main GWT library as well.

Original comment by cw...@cloudsherpas.com on 18 May 2011 at 12:22

GoogleCodeExporter commented 9 years ago
You might be interested in this project: 

  https://code.google.com/p/gwt-oauth2/

Original comment by zundel@google.com on 18 May 2011 at 12:56

GoogleCodeExporter commented 9 years ago
This looks promising. Thank you.

Original comment by cw...@cloudsherpas.com on 18 May 2011 at 5:19

GoogleCodeExporter commented 9 years ago

Original comment by zundel@google.com on 28 Oct 2011 at 4:30