【问题标题】:Strava V3 API Upload Using Scribe (on Google App Engine)使用 Scribe 上传 Strava V3 API(在 Google App Engine 上)
【发布时间】:2015-03-28 01:07:23
【问题描述】:

我正在尝试使用 Scribe 使用他们的 V3 API(使用 Java,在 Google App Engine 中)将 GPX 文件(未压缩)上传到 Strava:

String url = "https://www.strava.com/api/v3/uploads?access_token=<TOKEN>";
OAuthRequest req = new OAuthRequest(Verb.POST, url);
req.addQuerystringParameter("private", "1");
req.addQuerystringParameter("activity_type", "bike");
req.addQuerystringParameter("data_type", "gpx");
req.addQuerystringParameter("external_id", <Unique String>);

req.addHeader("Content-Type", "multipart/form-data");
String gpx = <Content of GPX file to Upload>;

req.addBodyParameter("file", gpx);

Response response = request.send();

结果是我从 Strava 收到响应代码 500(内部错误),并且它没有上传 GPX 活动。

我想这是我如何形成 HTTP 多部分 POST 的问题,它在 Strava 文档here 中定义为:

DEFINITION
POST https://www.strava.com/api/v3/uploads
EXAMPLE REQUEST
$ curl -X POST https://www.strava.com/api/v3/uploads \
-F access_token=83ebeabdec09f6670863766f792ead24d61fe3f9 \
-F activity_type=ride \
-F file=@test.fit \
-F data_type=fit


Parameters:
<OTHERS>
file: multipart/form-data required 
the actual activity data, if gzipped the data_type must end with .gz

请问有什么想法可以让我完成这项工作吗?谢谢。

编辑:通过我自己的进一步调查发现了几件事:

  • Scribe 主要是为了签署 oauth,所以作者并不专注于添加处理多部分/表单数据的功能(尽管有人建议添加此类功能)
  • 我可以通过 Scribe 使用其中一个 Apache 类 (MultipartEntity) 来执行此操作,但我认为 Google App Engine 不支持 Apache 库。见this线程,这将是完美的,除了Apache / GAE问题

【问题讨论】:

    标签: java google-app-engine scribe


    【解决方案1】:

    这是一个完整的解决方案。传递您的不记名令牌和文件名以上传。这适用于 FIT 文件,但对于 GPX 来说是一个简单的更改。取自这里:

    https://github.com/davidzof/strava-oauth/

    public static long uploadActivity(String bearer, String fileName) {
        JSONObject jsonObj = null;
    
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(
                "https://www.strava.com/api/v3/uploads");
        httpPost.addHeader("Authorization", "Bearer " + bearer);
        httpPost.setHeader("enctype", "multipart/form-data");
    
        MultipartEntity reqEntity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);
        try {
            reqEntity.addPart("activity_type", new StringBody("ride"));
            reqEntity.addPart("data_type", new StringBody("fit"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        FileBody bin = new FileBody(new File(fileName));
        reqEntity.addPart("file", bin);
    
        httpPost.setEntity(reqEntity);
    
        HttpResponse response;
        try {
            response = httpClient.execute(httpPost);
    
            HttpEntity respEntity = response.getEntity();
    
            if (respEntity != null) {
                // EntityUtils to get the response content
                String content = EntityUtils.toString(respEntity);
                System.out.println(content);
                JSONParser jsonParser = new JSONParser();
                jsonObj = (JSONObject) jsonParser.parse(content);
            }
        } catch (ParseException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    
        return (long) jsonObj.get("id");
    }
    

    【讨论】:

      【解决方案2】:

      最后我在 Stack Overflow 上找到了this solution,它提供了使用 Apache 类的方法

      MultipartEntity
      

      在 Google App Engine 的限制范围内。我只需要在我的 GAE 项目中添加三个 Apache JAR:httpclient-4.3.1.jar、httpcore-4.3.jar 和 httpmime-4.3.1.jar。

      然后我结合this 解决方案,允许我添加一个

      FileBody
      

      MultipartEntity
      

      (在我的例子中是一个 GPX“文件”,我在一个字符串中构造了一个)。这两种解决方案完美结合!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-24
        • 2014-05-12
        • 2011-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多