如何在oriceon / oauth-5-laravel twitter api中实现友谊/存在?

时间:2016-01-13 07:58:40

标签: oauth laravel-5 twitter-oauth

iam使用oriceon/oauth-5-laravel 。帮助我实施友谊/存在。我想发布关注特定人物的请求。帮助me.tysm advance

1 个答案:

答案 0 :(得分:2)

考虑一下您想在Twitter中关注 NASA 的情况。 NASA NASA 的屏幕名称。您应该添加屏幕名称到如下所示。将此方法添加到您的控制器并进行正确的路由。

public function followWithTwitter(Request $request)
{

    $token  = $request->get('oauth_token');
    $verify = $request->get('oauth_verifier');

    $tw = \OAuth::consumer('Twitter');
    if ( ! is_null($token) && ! is_null($verify))
    {
        // This was a callback request from twitter, get the token
        $token = $tw->requestAccessToken($token, $verify);
        // Send a request with it
        $result = json_decode($tw->request('https://api.twitter.com/1.1/friendships/create.json?screen_name=NASA&follow=true','POST'), true);
        if (!$result){
            //do some tasks for calculating and database updation for following
            return ("failed");
        }
        else{
            //do some tasks for calculating and database updation for following
            return ("success");
        }
    }
    // if not ask for permission first
    else
    {

        // get request token
        $reqToken = $tw->requestRequestToken();
        // get Authorization Uri sending the request token
        $url = $tw->getAuthorizationUri(['oauth_token' => $reqToken->getRequestToken()]);

        // return to twitter login url
        return redirect((string)$url);
    }
}