Xamarin.Auth AddMultipartData调用Strava上传接口失败,数据未发送原因排查
Let's break down why your upload request is failing with that confusing error response, even though you're using the correct file parameter as per Strava's documentation.
The Root Problem
Looking closely at your code, there's a critical variable mismatch in your multipart data setup:
FileStream fs = new FileStream(fileName, FileMode.Open); var request = new OAuth2Request("POST", new Uri("https://www.strava.com/api/v3/uploads"), null, StravaAccount); // You're passing 'input' here, but you opened the file stream into 'fs' request.AddMultipartData("file", input, "application/gpx+xml", Uri.EscapeDataString(fileName));
You initialized a FileStream named fs pointing to your GPX file, but when calling AddMultipartData, you're passing a variable called input instead of fs. This means you're not actually sending any file content to Strava—hence the (misleading) "empty data" error they're returning.
Fixed Code
Replace input with fs in the AddMultipartData call, and use a using block to properly dispose the stream (best practice to avoid resource leaks):
using (FileStream fs = new FileStream(fileName, FileMode.Open)) { var request = new OAuth2Request("POST", new Uri("https://www.strava.com/api/v3/uploads"), null, StravaAccount); request.AddMultipartData("file", fs, "application/gpx+xml", Uri.EscapeDataString(fileName)); Response response = await request.GetResponseAsync(); }
Quick Additional Checks
- Confirm
fileNamepoints to a valid, non-empty GPX file on your device. - Make sure your
StravaAccounthas the requiredwritescope (mandatory for upload operations). - Double-check the MIME type matches your file format (
application/gpx+xmlis correct for GPX files; adjust if you're using TCX/FIT).
内容的提问来源于stack exchange,提问作者Chris

