PHP Twitter OAuth - 我無法驗證我的帳戶 (PHP Twitter OAuth - I cannot authenticate my account)


問題描述

PHP Twitter OAuth - 我無法驗證我的帳戶 (PHP Twitter OAuth - I cannot authenticate my account)

I am struggling whole day with implementation of Twitter authentication (with using OAuth), but I am already desperate, I am still getting the error message Could not authenticate you., it's becoming my nightmare.

Here are my two files which I use for authentication:

requestOauth.php

session_start();
$basedir=dirname(__FILE__)."";  
include($basedir.'/config.php');
include_once( $basedir.'/twitteroauth/twitteroauth.php' );

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);    
$request_token = $connection->getRequestToken(OAUTH_CALLBACK);

//$_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
//$_SESSION['oauth_token_secret'] = $secret = $request_token['oauth_token_secret'];
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; 

switch ($connection->http_code) {
  case 200:

    $url = $connection->getAuthorizeURL($_SESSION['oauth_token'], FALSE);
    header('Location: ' . $url); 
    break;
  default:
    echo 'Could not connect to Twitter. Refresh the page or try again later.';
}

index.php

if(!isset($_GET['oauth_verifier'])){
    $link_redirect = '<a href="requestOauth.php">Login With Twitter</a>';
}
else
{
    if(isset($_SESSION['access_token'])){ 
        $oauth_token = $_SESSION['access_token']['oauth_token'];
        $oauth_token_secret = $_SESSION['access_token']['oauth_token_secret'];
        $twitterRequest = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $oauth_token, $oauth_token_secret);

    } else {

        $twitterRequest = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
        $accessToken = $twitterRequest->getAccessToken($_GET['oauth_verifier']);
        $_SESSION['access_token'] = $accessToken;
        echo 'ACCESS TOKEN2: '.var_dump($_SESSION['access_token']);
    }

    $user_info = $twitterRequest->get('account/verify_credentials');
    if(isset($user_info->error))
    {
        echo $user_info->error;
        //header("location: requestOauth.php");
    }  else {
        var_dump($_GET);
    }

I am still getting the error Could not authenticate you. and already have no idea, where could be something wrong...

Could you please give me any help, how to figure out this problem?

Thank you 


參考解法

方法 1:

According to the documentation, step 8 you have to build another TwitterOAuth object after calling getAccessToken with the values you get from there.

In your case something like this could work:

$twitterRequest = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $accessToken['oauth_token'], $accessToken['oauth_token_secret']);

(by user984621Jan Gerlinger)

參考文件

  1. PHP Twitter OAuth - I cannot authenticate my account (CC BY-SA 3.0/4.0)

#authentication #twitter #OAuth #PHP






相關問題

PHP Twitter OAuth - 我無法驗證我的帳戶 (PHP Twitter OAuth - I cannot authenticate my account)

如何設置 RIA 服務以使用現有的 ASP.Net 成員基礎 (How to setup RIA Services to use an existing ASP.Net membership base)

使用具有身份驗證的 URL 設置圖像-IOS SDK (Set Image With URL having authentication-IOS SDK)

支持 Facebook 的 Codeigniter 身份驗證庫 (Codeigniter Authentication library with Facebook support)

lighttpd:身份驗證後如何將端口(僅對本地主機可見)轉發到 WAN? (lighttpd: How to forward port (visible only to localhost) to WAN after authentication?)

Symfony 2.8 WebTestCase:無法創建登錄會話 (Symfony 2.8 WebTestCase: Cannot create logged-in session)

使用 facebook、twitter 等進行身份驗證。合而為一! (Authentication with facebook, twitter, etc.. all in one!)

默認情況下,ASP.NET 登錄控件是否使用 ReturnURL 進行重定向? (By default, does ASP.NET Login Control use the ReturnURL to redirect?)

發布/訂閱請求身份驗證作為服務(或通過服務密鑰)而不是 Auth0 臨時密鑰 (Pub/sub request authentication as a service (or by service key) instead of Auth0 temporary key)

通過 Nginx 進行 Haproxy 身份驗證 (Haproxy authentication through Nginx)

Django login() 身份驗證總是返回 None (Django login() authenticate always returns None)

laravel 微風 Multi Auth - 具有兩個不同註冊的 Admin Guard (laravel breeze Multi Auth - Admin Guard with two diffirent registration)







留言討論