Bungie-net / api

Resources for the Bungie.net API
Other
1.22k stars 92 forks source link

Trouble with getting TransferItem to work #1456

Closed ChaisSt closed 3 years ago

ChaisSt commented 3 years ago

I've been having trouble with getting the TransferItem endpoint to work and I'm not sure what the issue exactly is. The error message I get back is very vague, just a pretty basic internal server error which doesn't point to what the exact issue is... I'm creating the mobile app in Xamarin(C#), below is some relevant code.

public async Task TransferItemAsync(ItemModel item, MemberModel member, long characterId)
{
       bool location = GetLocation(item);  //returns true/false depending on where the item currently is 
       TransferModel postBody = new TransferModel
       {
              itemReferenceHash = (uint)item.ItemInstance,
              stackSize = 1,
              transferToVault = location,
              itemId = item.ItemHashId,
              characterId = characterId,
              membershipType = (int)member.MemberType
       };
       string uri = "https://www.bungie.net/Platform/Destiny2/Actions/Items/TransferItem/";
       await APIAccessor.TransferAsync<TransferModel>(uri, postBody);
}

public static async Task<dynamic> TransferAsync<TResult>(string uri, TransferModel postBody)
{
      string json = JsonConvert.SerializeObject(postBody);
      var content = new StringContent(json, Encoding.UTF8, "application/json");
      var response = await ApiClient.PostAsync(uri, content);

     string serialized = await response.Content.ReadAsStringAsync();
      return serialized;
}

ApiClient contains all the required headers that I know of(including oauth). I've checked all the variables to make sure they're getting populated. I've also checked string json and it looks fine to me. I've removed encoding and application/json from var content and the issue still exists.... I'm just kind of lost on where I'm going wrong.

Cytraen commented 3 years ago

You should post what the error is, no matter how vague it might be. It's better than nothing. Outside of that, looks fine to me.
Have you considered using something like BungieSharper to handle the client implementation for you?

ChaisSt commented 3 years ago

I have not but honestly at this point I'm almost done with the project, I just need to get the transfer working on it and it'll be finished. Here's the error: {StatusCode: 500, ReasonPhrase: 'InternalServerError', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Access-Control-Allow-Origin: https://www.bungie.net Cache-Control: max-age=0, private CF-Cache-Status: DYNAMIC CF-RAY: 6411059fc957c7d1-DEN cf-request-id: 097e81d7dd0000c7d1f1272000000001 Connection: keep-alive Date: Fri, 16 Apr 2021 22:59:04 GMT Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" Server: cloudflare Vary: Origin X-Android-Received-Millis: 1618613945261 X-Android-Response-Source: NETWORK 500 X-Android-Selected-Protocol: http/1.1 X-Android-Sent-Millis: 1618613945166 X-BungieNext-MID2: 103 X-BungieNext-Renderer: Frog Blast the Ventcore X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-SelfUrl: https://www.bungie.net/Platform/Destiny2/Actions/Items/TransferItem/ X-UA-Compatible: IE=edge X-Ventcore-Status: 3 Content-Length: 166 Content-Type: application/json; charset=utf-8 Expires: -1 }}

Cytraen commented 3 years ago

Wait, I see your problem now. In your TransferModel your itemId should be the instance ID, and your itemReferenceHash should be the item hash. It looks like you've got them swapped.

For the record, this works for me:

var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("X-API-Key", apiKey);
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);

var request = new DestinyItemTransferRequest
{
    ItemReferenceHash = item.ItemHash,
    StackSize = 1,
    TransferToVault = false,
    ItemId = item.ItemInstanceId,
    CharacterId = hunterCharacterId,
    MembershipType = memberType
};

var stringMsg = JsonSerializer.Serialize(request);
var result = await httpClient.PostAsync("https://www.bungie.net/Platform/Destiny2/Actions/Items/TransferItem/", new StringContent(stringMsg));
ChaisSt commented 3 years ago

It worked! Thank you so much for pointing that out to me, I really appreciate you!