kebabtent / pogoprotos-php

Compiled PHP protobufs for pokemon go
MIT License
22 stars 8 forks source link

API Implementation #3

Closed barryvdh closed 8 years ago

barryvdh commented 8 years ago

Is this actually implemented already somewhere?

I was trying to use this with https://github.com/DrDelay/pokemon-go but couldn't really figure out how to properly do it all. Are you working on something, or is that private?

Otherwise might be an idea to combine? /cc @DrDelay

This wat I was working on, based on https://github.com/ctrlaltdylan/pgo-php/blob/master/app/Models/Http/Client.php and https://github.com/tejado/pgoapi)

use POGOProtos\Networking\Envelopes\RequestEnvelope;
use POGOProtos\Networking\Envelopes\RequestEnvelope_AuthInfo;
use POGOProtos\Networking\Envelopes\RequestEnvelope_AuthInfo_JWT;
use POGOProtos\Networking\Envelopes\ResponseEnvelope;
use POGOProtos\Networking\Requests\Messages\GetPlayerMessage;
use POGOProtos\Networking\Requests\Request;
use POGOProtos\Networking\Requests\RequestType;

$requests = [];

$req = new Request();
$req->setRequestType(RequestType::GET_PLAYER);
$requests[] = $req;

$message = new DownloadSettingsMessage();
$message->setHash('4a2e9bc330dae60e7b74fc85b98868ab4700802e');
$req = new Request();
$req->setRequestType(RequestType::DOWNLOAD_SETTINGS);
$req->setRequestMessage($message->toProtobuf());
$requests[] = $req;

$token = new RequestEnvelope_AuthInfo_JWT();
$token->setContents($this->accessToken);
$token->setUnknown2(59);

$authInfo = new RequestEnvelope_AuthInfo();
$authInfo->setProvider('google');
$authInfo->setToken($token);

$envelope = new RequestEnvelope();
$envelope->addAllRequests($requests);
$envelope->setAuthInfo($authInfo);
$envelope->setRequestId(8145806132888207460);
$envelope->setStatusCode(2);
$envelope->setLatitude(51.123);
$envelope->setLongitude(5.123);
$envelope->setAltitude(0);

$client = $this->container->get(GuzzleClient::class);

$response = $client->request('POST', $this->apiUrl, ['body' => $envelope->toProtobuf()]);
$responseEnvelope = new ResponseEnvelope((string) $response->getBody());

But response seems to be just 'd"', not an actual proper something, or even an error message.. (Also saw that you already fixed the use ProtobufIO which I also ran into :))

jaspervdm commented 8 years ago

I am working on a PHP API that uses this, I managed to get the API endpoint, but it requires some modifications to these classes, see also #2 . I will put up some code in https://github.com/jaspervdm/pogoapi-php that works upto now

jaspervdm commented 8 years ago

I committed my progress to https://github.com/jaspervdm/pogoapi-php , I would be very open to a collaboration to develop a full API!

DrDelay commented 8 years ago

Looks very promising, I didn't deal with the whole Protobuf API communication at all until now, but will definitely try to understand and get some basic calls to work with this repo tomorrow. :+1:

DrDelay commented 8 years ago

I tried to integrate your protos into my project (see DrDelay/pokemon-go@bfd6f82997f4aaabf644701d2264dd08cf36f4c3), but am also only getting the 'd"' as response. Did anyone here solve that already :question:

barryvdh commented 8 years ago

Using pogoapi-php, I was able to get the API endpoint yesterday, but today it's giving me trouble. Do get more then 'd"' though: https://github.com/jaspervdm/pogoapi-php/issues/4

barryvdh commented 8 years ago

When you fix the auth name, you should get more then d": https://github.com/DrDelay/pokemon-go/pull/2 (But then you get the same as the issue I referenced)

barryvdh commented 8 years ago

Should be fixed with https://github.com/jaspervdm/pogoprotos-php/pull/4

DrDelay commented 8 years ago

Can confirm this works for me

barryvdh commented 8 years ago

Only the results array is still empty (1 item with null, because of the 0 length)

DrDelay commented 8 years ago

I don't know if it's obvious and everyone already did that, but when you decode the raw response this is empty aswell:

$ protoc --decode_raw < raw.bin
1: 53
2: 1469378659230941184
3: "pgorelease.nianticlabs.com/plfe/193"
7 {
  1: "\033\363/\364C\17....\354\261"
  2: 1469378213466
  3: "\303\2....;\353t\300"
}
100: ""

... -> Removed data from the AuthTicket, my PKMGO account is really precious :laughing:

So we are doing something wrong in the request?

DrDelay commented 8 years ago

I compared the requests from a working C#-Implementation with this one, and there is a difference that might be the problem: The working one sends the location (in this example longitude) like this: 4632568534467118047 "Ours" sends it like this: 0x40485e646c9122d3

The other one actually calls a function named FloatAsUlong to convert the data. In a 64-bit PHP Environment you can implement this like that: unpack('Q', pack('d', $float))[1]

I tried to implement this, but the Proto-file internally calls Protobuf::write_double that makes it wrong again. There is actually a comment saying BUG We need to convert from machine order to little order first next to this function.

This is how far I got, hopefully it helps someone to find a solution to this.

barryvdh commented 8 years ago

Okay, I was actually looking in to that this afternoon, why the Python API was doing that here https://github.com/tejado/pgoapi/blob/master/pgoapi/pgoapi.py

def set_position(self, lat, lng, alt):   
        self.log.debug('Set Position - Lat: %s Long: %s Alt: %s', lat, lng, alt)

        self._position_lat = f2i(lat)
        self._position_lng = f2i(lng)
        self._position_alt = f2i(alt)

def f2i(float):
  return struct.unpack('<Q', struct.pack('<d', float))[0]

I tried passing the raw value from that, but I didn't check the actual sent data.

barryvdh commented 8 years ago

Also, the note seems to be about little/big endianness. Which they check here for example: https://github.com/drslump/Protobuf-PHP/blob/2201961982bc531a9ab22fc8a150ba815e9b6ef0/library/DrSlump/Protobuf/Codec/Binary/Writer.php#L287-L299 But that doesn't seem to matter for my case.

So should we fix Protobuf::write_double or is the type wrong?

NicklasWallgren commented 8 years ago

I been working on a library for a while, have a look.

https://github.com/NicklasWallgren/PokemonGoAPI-PHP

barryvdh commented 8 years ago

Did you also run in to the above issue, or how did you solve that?

NicklasWallgren commented 8 years ago

I did not actually, take a look at 'NicklasW\PkmGoApi\Handlers\RequestHandler' to see how I handle the API requests.

jaspervdm commented 8 years ago

I noticed you create the RequestEnvelope in RequestHandler, but never fill in the location (latitude/longitude/altitude). Are you planning on implementing that?

NicklasWallgren commented 8 years ago

In time, it's on the todo list :)

DrDelay commented 8 years ago

I feel a little dumb right know. You have to resend the request with the AuthTicket after receiving it.

He is doing it there: https://github.com/NicklasWallgren/PokemonGoAPI-PHP/blob/b1174471a1039c89a23260eedb7f6f45430d8f4d/src/Handlers/RequestHandler.php#L90-L95