instagram-api icon indicating copy to clipboard operation
instagram-api copied to clipboard

Add and remove close friend.

Open hussainsheikh048 opened this issue 3 years ago • 26 comments

Hi,

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

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

hussainsheikh048 avatar Jul 12 '21 11:07 hussainsheikh048

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

NicklasWallgren avatar Jul 12 '21 18:07 NicklasWallgren

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;
    }

hussainsheikh048 avatar Jul 13 '21 05:07 hussainsheikh048

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);
        });
    }

NicklasWallgren avatar Jul 13 '21 07:07 NicklasWallgren

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

hussainsheikh048 avatar Jul 15 '21 10:07 hussainsheikh048

Hi Nick,

Imran's here, with my another account.

imran-saleem834 avatar Jul 15 '21 10:07 imran-saleem834

@hussainsheikh048 Does the ApiResponseException contain any descriptive error message?

NicklasWallgren avatar Jul 18 '21 06:07 NicklasWallgren

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;

hussainsheikh048 avatar Jul 18 '21 17:07 hussainsheikh048

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?

NicklasWallgren avatar Jul 18 '21 19:07 NicklasWallgren

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.

imran-saleem834 avatar Jul 18 '21 20:07 imran-saleem834

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.

NicklasWallgren avatar Jul 19 '21 06:07 NicklasWallgren

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

imran-saleem834 avatar Jul 19 '21 06:07 imran-saleem834

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

hussainsheikh048 avatar Jul 20 '21 15:07 hussainsheikh048

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.

NicklasWallgren avatar Jul 20 '21 15:07 NicklasWallgren

But it didn’t fill auto code

Sent from my iPhone

On 20-Jul-2021, at 8:11 PM, NicklasWallgren @.***> wrote:

 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.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

imran-saleem834 avatar Jul 20 '21 15:07 imran-saleem834

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

imran-saleem834 avatar Jul 24 '21 17:07 imran-saleem834

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

NicklasWallgren avatar Jul 25 '21 06:07 NicklasWallgren

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());
}

imran-saleem834 avatar Jul 26 '21 05:07 imran-saleem834

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 avatar Jul 26 '21 06:07 NicklasWallgren

Have you managed to get it working?

NicklasWallgren avatar Jul 29 '21 09:07 NicklasWallgren

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

imran-saleem834 avatar Jul 29 '21 14:07 imran-saleem834

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

xsinisa avatar Aug 05 '21 21:08 xsinisa

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] => 
)

hussainsheikh048 avatar Aug 07 '21 06:08 hussainsheikh048

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

NicklasWallgren avatar Aug 07 '21 13:08 NicklasWallgren

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

@NicklasWallgren I can create PR, please give me permissions

xsinisa avatar Aug 08 '21 16:08 xsinisa

@xsinisa Thanks, feel free to open a PR. https://github.com/NicklasWallgren/instagram-api/pulls

NicklasWallgren avatar Aug 08 '21 17:08 NicklasWallgren

@xsinisa is it 2fa working for you?

imran-saleem834 avatar Aug 09 '21 03:08 imran-saleem834