Curl OAuth与GWT对抗Strava API

Curl OAuth与GWT对抗Strava API,curl,gwt,oauth,Curl,Gwt,Oauth,我正在尝试根据StravaAPI创建一个GWT应用程序。 首先要做的是身份验证 他们说,对于代币交换,您必须执行以下操作: curl -X POST https://www.strava.com/oauth/token \ -F client_id=5 \ -F client_secret=7b2946535949ae70f015d696d8ac602830ece412 \ -F code=75e251e3ff8fff final FormPanel form = ne

我正在尝试根据StravaAPI创建一个GWT应用程序。 首先要做的是身份验证

他们说,对于代币交换,您必须执行以下操作:

curl -X POST https://www.strava.com/oauth/token \
   -F client_id=5 \
   -F client_secret=7b2946535949ae70f015d696d8ac602830ece412 \
   -F code=75e251e3ff8fff
   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();            
     }
  });
据我所知,那些-F表示各种形式帖子中的字段? 所以我创造了这样的东西:

curl -X POST https://www.strava.com/oauth/token \
   -F client_id=5 \
   -F client_secret=7b2946535949ae70f015d696d8ac602830ece412 \
   -F code=75e251e3ff8fff
   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
现在问题来了。通过创建一个表单来模拟旋度示例,我是否正确?
帧错误是否与使用IFRAME的GWT有关?如何解决此问题?

Strava正在为其对您的响应设置一个标题,该标题不允许在iframe中加载它(请参阅)。我假设您的GWT应用程序正在一个小时内加载此表单

在进一步阅读时,他们也描述了这个过程,我看到这就是您找到示例的地方
curl

完成令牌交换

如果用户接受共享对其Strava数据访问的请求,Strava将使用授权代码重定向回redirect_uri。应用程序现在必须使用其客户端ID和客户端机密将临时授权代码交换为访问令牌

您可能需要考虑使用。您可以通过在生成器上使用
setHeader(“内容类型”、“应用程序/x-www-form-urlencodedata”)
对表单数据进行编码来设置表单数据。您在那里的回调可以负责接受令牌以在其他地方使用

示例部分来自缓冲区并写入缓冲区,未测试:

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和客户端密码这个秘密永远不应该被分享。

如果这是一个供公众使用的应用程序,则不应使用上述代码。您必须在服务器上执行此部分