2016-09-15 113 views
0

我使用Socialite使用Twitter登錄並使用Abrahams的TwitterOauth使API工作。在laravel,我給自己定的API密鑰信息:如何在Twitter中使用API​​獲取當前登錄的用戶詳細信息

'twitter' => [ 
     'client_id'  => '**************', 
     'client_secret' => '**************', 
     'access_token'  => '***************', 
     'access_token_key' => '***************', 
     'redirect'   => '***********', 
    ], 

要連接到Twitter我用:使用社交名媛

public function getTwitterConnection() 
    { 
     $this->connection = new TwitterOAuth(Config::get('services.twitter.client_id'), Config::get('services.twitter.client_secret'), Config::get('services.twitter.access_token'), Config::get('services.twitter.access_token_key')); 
    } 

,我能夠讓用戶通過他們的憑據登錄。但是,當我嘗試訪問:

public function getCurrentLoggedInUser() { 
     $this->getTwitterConnection(); 
     return $this->connection->get("account/verify_credentials"); 
    } 

我收到開發人員的Twitter帳戶,即我的信息。 我認爲我無法在Socialite和TwitterOauth之間進行同步。 我該如何解決這個問題?請建議我獲取登錄用戶信息的更好方法。

回答

1

我知道這很晚了。但無論如何,這是答案。

'access_token'  => '***************', 
    'access_token_key' => '***************', 

您不需要包含這些詳細信息。這access_tokenaccess_token_key屬於您自己的帳戶,這就是爲什麼你得到您的帳戶信息,而不是登錄的用戶。您必須使用登錄用戶的access_tokenaccess_token_key,所以這不應該由您預先填寫。

正在處理Twitter的回調重定向控制器內部:

// get logged in twitter user 
    $user = Socialite::driver('twitter')->user(); 

    // OAuth One Providers 
    $token = $user->token; // this is access_token that you need 
    $tokenSecret = $user->tokenSecret; // this is the access_token_key that you need 

    // now call TwitterOauth with all the details 
    // and then you can send request to account/verify_credentials 
0

這些代碼對我幫助很大:

對於登錄:

public function redirectToTwitter() // AuthController.php 
{ 
    $t = new Twitter(); 
    $t->sessionDestroy(); 
    return redirect($t->getAuthUrl()); 
} 

public function handleTwitterCallback(Request $request) 
{ 
    $t = new Twitter(); 
    $oauth = $request->all(); 
    $user = $t->getCurrentLoggedInUser($oauth); 
} 

進行驗證:Twitter.php

public function getTwitterConnection(){ 
    $oauth_token = Session::get('oauth_token'); 
    $oauth_token_secret = Session::get('oauth_token_secret'); 
    if(!empty($oauth_token) && !empty($oauth_token_secret)) { 
     $this->connection = new TwitterOAuth(Config::get('services.twitter.client_id'), Config::get('services.twitter.client_secret'), $oauth_token, $oauth_token_secret); 
    } 
    //return $this->connection->get("account/verify_credentials"); 
} 

public function getAuthUrl() { 
    $this->getAuthConnection(); 
    $token = $this->connection->oauth("oauth/request_token", array("oauth_callback" => Config::get('services.twitter.redirect'))); 
    Session::put($token); 
    $url = $this->connection->url("oauth/authorize", ['oauth_token' => $token['oauth_token']]); 
    return $url; 
} 

public function getCurrentLoggedInUser($oauth) { 
    $this->getTwitterConnection(); //gets oauth for developers 
    $access = $this->connection->oauth("oauth/access_token", array("oauth_verifier" => $oauth['oauth_verifier'])); 
    Session::put('oauth_token', $access['oauth_token']); 
    Session::put('oauth_token_secret', $access['oauth_token_secret']); 
    $this->getTwitterConnection(); //after login gets oauth for logged user 
    return $this->connection->get("account/verify_credentials"); 
} 
1

適用於推特和社交名流 使用

$user = Socialite::driver('twitter')->userFromTokenAndSecret($token,$secret); 
相關問題