我正在尝试针对Strava API创建一个GWT应用程序。首先要做的是身份验证。
在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表示多表单post中的字段?所以我创建了类似这样的东西:
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有关?我该如何解决这个问题?
发布于 2017-06-04 05:09:23
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并写入缓冲区,未经过测试:
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和客户端密码。这个秘密永远不应该被分享。
如果这是一个面向公众的应用程序,那么您就不应该使用上面的代码。您必须在您的服务器上执行此部分。
https://stackoverflow.com/questions/44316239
复制相似问题