Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add and remove close friend. #49

Open
hussainsheikh048 opened this issue Jul 12, 2021 · 26 comments
Open

Add and remove close friend. #49

hussainsheikh048 opened this issue Jul 12, 2021 · 26 comments

Comments

@hussainsheikh048
Copy link

Hi,

Can we add/remove followers to close friends I've checked dilame repository supported.

https://github.com/dilame/instagram-private-api

@NicklasWallgren
Copy link
Owner

What methods are you referring to? You're able to add and remove followers.

Have a look at https://github.com/NicklasWallgren/instagram-api/blob/master/src/Traits/MakeFriendshipsRequestsAccessible.php

@hussainsheikh048
Copy link
Author

No, I'm not referring to add and remove followers, Instagram has a new feature to add a friend into a close friend list.

I've tried to add a function inside your library to see if it's working or not, but my bad it's not working...

MakeFriendshipsRequestsAccessible.php
public function setBestFriend(): FollowingResponse
 {
    return PromiseUtils::wait($this->getClient()->setBestFriend());
 }
 
 FriendshipsFeaturesTrait.php
 public function setBestFriend(): PromiseInterface
    {
        return $this->authenticated(function (): PromiseInterface {
            $request = $this->buildRequest('friendships/set_besties', new FollowResponse())
                ->addCSRFTokenAndUserId($this->session)
                ->addUuid($this->session)
                 ->addPayloadParam('user_id', '2108351612')
                 ->addPayloadParam('add', '2108351612')
//                ->addQueryParam('add', ['2108351612'])
                ->setPayloadSerializerType(PayloadSerializerFactory::TYPE_SIGNED);

            return $this->call($request);
        });
    }

I've found the API code from the NODE API.

async setBesties(input = {}) {
        const { body } = await this.client.request.send({
            url: `/api/v1/friendships/set_besties/`,
            method: 'POST',
            form: this.client.request.sign({
                _csrftoken: this.client.state.cookieCsrfToken,
                _uid: this.client.state.cookieUserId,
                device_id: this.client.state.deviceId,
                _uuid: this.client.state.uuid,
                module: 'favorites_home_list',
                source: 'audience_manager',
                add: input.add,
                remove: input.remove,
            }),
        });
        return body.friendship_statuses;
    }

@NicklasWallgren
Copy link
Owner

Alright, the following example seems to work. You are welcome to create a PR.

The response looks like this.

{
   "friendship_statuses":{
      "2108351612":{
         "following":true,
         "is_private":false,
         "incoming_request":false,
         "outgoing_request":false,
         "is_blocking_reel":false,
         "is_muting_reel":false,
         "is_bestie":true,
         "is_restricted":false
      }
   },
   "status":"ok"
}

MakeFriendshipsRequestsAccessible

    /**
     * @param array $userIds
     * @return FollowingResponse
     */
    public function addBestFriend(array $userIds): FollowResponse
    {
        return PromiseUtils::wait($this->getClient()->addBestFriend($userIds));
    }

FriendshipsFeaturesTrait

    /**
     * @param array $userIds
     * @return PromiseInterface
     */
    public function addBestFriend(array $userIds): PromiseInterface
    {
        return $this->authenticated(function () use ($userIds): PromiseInterface {
            $request = $this->buildRequest('friendships/set_besties/', new FollowResponse())
                ->addPayloadParam('module', 'favorites_home_list')
                ->addPayloadParam('source', 'audience_manager')
                ->addCSRFTokenAndUserId($this->session)
                ->addUuid($this->session)
                ->addPayloadParam('add', $userIds)
                ->addPayloadParam('remove', [])
                ->setPayloadSerializerType(PayloadSerializerFactory::TYPE_SIGNED);

            return $this->call($request);
        });
    }
    /**
     * @param array $userIds
     * @return PromiseInterface
     */
    public function removeBestFriend(array $userIds): PromiseInterface
    {
        return $this->authenticated(function () use ($userIds): PromiseInterface {
            $request = $this->buildRequest('friendships/set_besties/', new FollowResponse())
                ->addPayloadParam('module', 'favorites_home_list')
                ->addPayloadParam('source', 'audience_manager')
                ->addPayloadParam('block_on_empty_thread_creation', 'false')
                ->addCSRFTokenAndUserId($this->session)
                ->addUuid($this->session)
                ->addPayloadParam('add', [])
                ->addPayloadParam('remove', $userIds)
                ->setPayloadSerializerType(PayloadSerializerFactory::TYPE_SIGNED);

            return $this->call($request);
        });
    }

@hussainsheikh048
Copy link
Author

Hi Nicklaws,

Thanks for your response.

I'm still getting errors.

Uncaught Instagram\SDK\Exceptions\ApiResponseException in E:\xampp\htdocs\instagram-api-master\src\Response\Serializers\AbstractResponseSerializer.php:137

@imran-saleem834
Copy link

Hi Nick,

Imran's here, with my another account.

@NicklasWallgren
Copy link
Owner

NicklasWallgren commented Jul 18, 2021

@hussainsheikh048 Does the ApiResponseException contain any descriptive error message?

@hussainsheikh048
Copy link
Author

hussainsheikh048 commented Jul 18, 2021

Fatal error:  Uncaught Instagram\SDK\Exceptions\ApiResponseException in E:\xampp\htdocs\instagram-api-master\src\Response\Serializers\AbstractResponseSerializer.php:137
Stack trace:
#0 E:\xampp\htdocs\instagram-api-master\src\Response\Serializers\AbstractResponseSerializer.php(59): Instagram\SDK\Response\Serializers\AbstractResponseSerializer::toException(NULL, Object(Instagram\SDK\Response\Responses\Friendships\FollowResponse))
#1 E:\xampp\htdocs\instagram-api-master\src\Client\Client.php(174): Instagram\SDK\Response\Serializers\AbstractResponseSerializer->decode(Object(GuzzleHttp\Psr7\Response), Object(Instagram\SDK\Client\Client))
#2 E:\xampp\htdocs\instagram-api-master\src\Utils\PromiseUtils.php(34): Instagram\SDK\Client\Client->Instagram\SDK\Client\{closure}()
#3 E:\xampp\htdocs\instagram-api-master\vendor\guzzlehttp\promises\src\TaskQueue.php(48): Instagram\SDK\Utils\PromiseUtils::Instagram\SDK\Utils\{closure}()
#4 E:\xampp\htdocs\instagram-api-master\vendor\guzzlehttp\guzzle\src\Handler\CurlMultiHandler.php(118): Gu in E:\xampp\htdocs\instagram-api-master\src\Response\Serializers\AbstractResponseSerializer.php on line 137

here is the full error, the message details are below. (I think the error is for the serializer, like we're passing an array to request something)

$responseInstance->getErrorType() is null;
$responseInstance->getMessage() is empty;

@NicklasWallgren
Copy link
Owner

NicklasWallgren commented Jul 18, 2021

Thanks. It's a bit strange, it works fine on my end, with the code posted above.

$instagram->addBestFriend(['2108351612']);

Could you dump the whole response body?

@imran-saleem834
Copy link

imran-saleem834 commented Jul 18, 2021 via email

@NicklasWallgren
Copy link
Owner

What php version you’re using?

Sent from my iPhone
On 19-Jul-2021, at 12:01 AM, NicklasWallgren @.***> wrote:  Thanks. It's a bit strange, it works fine on my end, with the code posted above. $instagram->addBestFriend(['2108351612']); — You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

Hey,
I'm running 7.2, but I don't think it's related to the issue. You are retrieving an API error, and the answer probably lies in the API response.

@imran-saleem834
Copy link

yea, it's working fine, i'll make PR soon. many thanks

@hussainsheikh048
Copy link
Author

Hi Nick, You've any idea how to bypass two-factor authentication?

@NicklasWallgren
Copy link
Owner

Hey, you won't be able to bypass 2fa, but it's fully possible to add 2fa support by implementing the accounts/two_factor_login/ api endpoint.

I don't have time to implement support for 2fa at the moment, so a PR is welcome.

@imran-saleem834
Copy link

imran-saleem834 commented Jul 20, 2021 via email

@imran-saleem834
Copy link

Hi Nick,

I was trying to make a request for 2fa, i figure it out we need to send identifier with the mobile code, but i can't see any identifier in the error response, i've attached the screenshot.

response

Here is instagram request where i dentifier the parameters.

Login request 404 response if 2fa enable:
login-request-404-response-if-2fa-enable

After given 2fa code request:
given-2fa-code-request

@NicklasWallgren
Copy link
Owner

Hey,

You can get hold of the 2fa identifier from the response of the accounts/login/ api endpoint.

The response includes two_factor_required as well as two_factor_info which contains the two_factor_identifier

@imran-saleem834
Copy link

Hi,

I want to hold 2fa identifier but i can't see identifier in the error response. here is response.

response-2

here is code for print the response:

try {
}
} catch (\Instagram\SDK\Exceptions\ApiResponseException $e) {
    echo "ApiResponseException";
    var_dump($e->getResponse());
}

@NicklasWallgren
Copy link
Owner

NicklasWallgren commented Jul 26, 2021

Hey,

The identifier is present in the raw json response, but is not specified in the SessionResponse class. You'll need to extend the class with new fields.

You can get hold of the raw response body at https://github.com/NicklasWallgren/instagram-api/blob/master/src/Response/Serializers/AbstractResponseSerializer.php#L56

@NicklasWallgren
Copy link
Owner

Have you managed to get it working?

@imran-saleem834
Copy link

I've not worked yet, but hopefully work tomorrow and make it work.

@xsinisa
Copy link

xsinisa commented Aug 5, 2021

I can confirm that @NicklasWallgren addition above is working...implemented and tested.

@hussainsheikh048
Copy link
Author

Hi Nick,

Still have an issue with the 2fa login,

MakeAccountRequestAccessible.php
public function twoFactorLogin(string $username, string $identifier, string $verificationCode): AuthenticatedUserResponse
{
      return PromiseUtils::wait($this->twoFactorLoginPromise($username, $identifier, $verificationCode));
}
public function twoFactorLoginPromise(string $username, string $identifier, string $verificationCode): PromiseInterface
{
      return $this->getClient()->twoFactorLogin($username, $identifier, $verificationCode);
}

Here is the actual function

AccountFeaturesTrait.php
public function twoFactorLogin(string $username, string $identifier, string $verificationCode): PromiseInterface
{
        return $this->headers()->then(function () use ($username, $identifier, $verificationCode): PromiseInterface {
            $request = $this->buildRequest('accounts/two_factor_login/', new SessionResponse());

            $payload = [
                'identifier'          => $identifier,
                'trust_signal'        => false,
                'username'            => $username,
                'verificationCode'    => $verificationCode,
                'verification_method' => 1,
                'queryParams'         => json_encode(['next' => '/']),
            ];

            print_r($payload);

            $request->setPayload($payload, PayloadSerializerFactory::TYPE_SIGNED)
                    ->setResponseSerializer(new LoginResponseSerializer($this->device, $this->client));

            return $this->call($request);
        });
}

That's still saying invalid parameter, is there something wrong?

https://i.instagram.com/api/v1/si/fetch_headers/
Array
(
    [identifier] => kqpGR0xtV2p1djEy2gF8Rm9UbXU1ZjNyTEUvRnBhL3ovVGpYaFVTR0lFQ1dFNmJsSTB0VXlaU1JwWUlxNEpScWI0SXZXdC9FWDdTTlNIOXRhQ1FEK3RnUENwcStsMUhXRXBQQUpWam5aSXgraGVSbk93WTRrTXZXZ1kvU0UzSEhwRVMrMmp2dU8ydTF5RGoxdjRISjV0a002ZldoOEZwMDRaY1Y5ZXRYOFJTL014dm1DS1R5QWlockhvQjc2dkRRcjc0T09ZZGszVzN0UCt6clY4cjRwRUU0NkJzbXRWd2ZBRDNidmlGRlBsWXFRY3NkSXZZRWl3TnQ5M3gzNkNYS1llVnJBaW95aW5GaXNMbHZJYkoxcjRYWXhhUHlBaXl3Z3FqVFVrTXllRUdNZng1L2hka0xsN3VobzFrYzVMdWR1c2Fxb005b041NXVGUXpkN3pXRlM1aHREY2ZtU3NrelhJL2lhNG5Da3pHeU1xZEF0a1lncVVMTXRnVDh5TklCOHdjR0FKd2R3QUE=
    [trust_signal] => 
    [username] => hussainsheikh4161
    [verificationCode] => 848241
    [verification_method] => 1
    [queryParams] => {"next":"\/"}
)
https://i.instagram.com/api/v1/accounts/two_factor_login/
0144Invalid ParametersInstagram\SDK\Response\Responses\User\SessionResponse Object
(
    [loggedInUser:Instagram\SDK\Response\Responses\User\SessionResponse:private] => 
    [session:Instagram\SDK\Response\Responses\User\SessionResponse:private] => 
    [status:protected] => fail
    [errorType:protected] => invalid_parameters
    [message:protected] => Invalid Parameters
    [invalidCredentials:protected] => 
)

@NicklasWallgren
Copy link
Owner

Hey,
I'll look into adding support for 2fa tomorrow.

@xsinisa
Copy link

xsinisa commented Aug 8, 2021

I can confirm that @NicklasWallgren addition above is working...implemented and tested.

@NicklasWallgren I can create PR, please give me permissions

@NicklasWallgren
Copy link
Owner

@imran-saleem834
Copy link

@xsinisa is it 2fa working for you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants