2017-06-01 34 views
0

我正在嘗試創建一個針對Strava API的GWT應用程序。 首先要做的是驗證。OAuth with GWT against Strava API

http://strava.github.io/api/v3/oauth/他們說,你必須做一些事情,如代幣兌換:

curl -X POST https://www.strava.com/oauth/token \ 
    -F client_id=5 \ 
    -F client_secret=7b2946535949ae70f015d696d8ac602830ece412 \ 
    -F code=75e251e3ff8fff 

據我知道這些事情-F代表的多種形式的後場? 因此,我創建類似:

final FormPanel form = new FormPanel(); 
    container.add(form); 
    form.setAction("https://www.strava.com/oauth/token"); 
    form.setEncoding(FormPanel.ENCODING_MULTIPART); 
    form.setMethod(FormPanel.METHOD_POST); 

    VerticalPanel panel = new VerticalPanel(); 
    form.setWidget(panel); 
    panel.add(new Hidden("client_id", CLIENT_ID)); 
    panel.add(new Hidden("client_secret", CLIENT_SECRET)); 
    panel.add(new Hidden("code", code)); 

    form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() 
    { 
     @Override 
     public void onSubmitComplete(SubmitCompleteEvent event) 
     { 
      GWT.log("complete " + event.getResults()); 
     } 
    }); 

    container.addAttachHandler(new AttachEvent.Handler() 
    { 

    @Override 
    public void onAttachOrDetach(AttachEvent event) 
    { 
     form.submit();    
    } 
    }); 

現在我做到這一點我看在Chrome開發工具以下錯誤時:

Refused to display 'https://www.strava.com/oauth/token' in a frame because it set 'X-Frame-Options' to 'deny'. 
FormPanelImpl.java:117 POST https://www.strava.com/oauth/token net::ERR_BLOCKED_BY_RESPONSE 

現在的問題是。我是否通過創建一個模擬該捲曲示例的表單來糾正? 這個幀錯誤與GWT使用IFRAME的東西有關嗎?我該如何解決 ?

回答

0

Strava正在對您的響應中設置一個標頭,禁止將其加載到iframe中(請參見How to set 'X-Frame-Options' on iframe?)。我假設你的GWT應用程序正在將這個表單加載到一箇中。

在進一步閱讀他們也描述了這個過程,我看到這就是你找到你的例子curl

完成令牌交換

如果用戶接受共享訪問他們的Strava數據的請求,將Strava回重定向到REDIRECT_URI與授權碼。應用程序現在必須使用其客戶端ID和客戶端密鑰來交換訪問令牌的臨時授權代碼。

您可能想使用RequestBuilder進行調查。您可以通過URL.encode以及setHeader("Content-Type", "application/x-www-form-urlencodeddata")在編輯器上對其進行編碼來設置表單數據。你的回調可以照顧接受令牌在其他地方使用。

例部分從GWT Server Communication ganked並寫入緩衝區,而不是測試:

String url = "https://www.strava.com/oauth/token"; 
RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, URL.encode(url)); 
builder.setHeader("Content-Type", "application/x-www-form-urlencodeddata"); 
String data = URL.encodeQueryString("client_id=5&client_secret=7b2946535949ae70f015d696d8ac602830ece412&code=75e251e3ff8fff"); 
try { 
    Request request = builder.sendRequest(data, new RequestCallback() { 
    public void onError(Request request, Throwable exception) { 
     // Couldn't connect to server (could be timeout, SOP violation, etc.) 
    } 

    public void onResponseReceived(Request request, Response response) { 
     if (200 == response.getStatusCode()) { 
      // Process the response in response.getText() 
     } else { 
     // Handle the error. Can get the status text from response.getStatusText() 
     } 
    } 
    }); 
} catch (RequestException e) { 
    // Couldn't connect to server 

已經沒有測試過這一點,我不知道上面是發送請求數據的適當方式,所以你可能需要把那部分弄清楚。

我們有一個額外的皺紋,但:

所有開發者需要在開始之前註冊自己的應用程序。已註冊的應用程序將被分配一個客戶端ID和客戶端SECRET。 絕不應該共享祕密。

如果這是一般公衆使用的應用程序,則不應使用上述代碼。您將不得不在服務器上執行此部分。