php
  • facebook
  • graph
  • upload
  • image
  • 2012-05-12 26 views 0 likes 
    0

    我只是試圖使用PHP將圖像上傳到用戶的時間軸。安裝我的應用程序後,我使用以下代碼,但得到錯誤:OAuthException:必須使用活動訪問令牌來查詢有關當前用戶的信息。使用PHP將圖片上傳到Facebook時出錯:OAuthException:必須使用有效的訪問令牌來查詢有關當前用戶的信息。

    我能夠通過$ token變量回顯訪問令牌,所以我知道它在那裏。我到處尋找,並試圖解決它,但沒有成功。繼承人的代碼...

    <?php 
    
    include_once "src/facebook.php"; 
    $app_id = 'xxxxxx'; 
    $application_secret = 'xxxxxx'; 
    
    $facebook = new Facebook(array( 
    'appId' => $app_id, 
    'secret' => $application_secret, 
    'cookie' => true, // enable optional cookie support 
    )); 
    if ($facebook->getUser()) 
    { 
        $user = $facebook->getUser(); 
        //die($token); 
    
         try 
         { 
          $token = $facebook->getAccessToken(); 
          $facebook->setFileUploadSupport(true); 
          $file = "5star.png"; 
          $post_data = array(
          "message" => "My photo caption", 
          "source" => '@' . realpath($file) 
          ); 
          //die("/me/photos/?access_token=$token"); 
          $data['photo'] = $facebook->api("/me/photos?access_token=$token", 'post', $post_data); 
         } catch (FacebookApiException $e) 
         { 
          die($e); 
         } 
    
    header('Location: http://google.com') ; 
    }else 
    { 
        $loginUrl = "https://graph.facebook.com/oauth/authorize?type=user_agent&display=page&client_id=$app_id&redirect_uri=http://apps.facebook.com/yourprofilerating/&scope=user_photos,email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown"; 
        echo "<script> top.location.href='" . $loginUrl . "'</script>"; 
    } 
    
    ?> 
    
    +0

    http://stackoverflow.com/questions/6034813/facebook-uncaught-oauthexception-an-active-access-token-must-be-used-to-query-i –

    回答

    1

    您可能有一個無效或過期的訪問令牌,檢出Handling Invalid and Expired Access Tokens。在api調用之前和$ token聲明之後插入本文中提供的代碼。

    +0

    我不認爲這是問題。只要我登錄Facebook並訪問應用程序,訪問令牌是否有效?即時安裝應用程序並立即運行此代碼。 – xendi

    +0

    如果你看看你鏈接到的頁面,你會看到我遇到的錯誤不是列出的錯誤之一。 – xendi

    +0

    在您的應用的「身份驗證對話框」的「擴展權限」部分中,是否需要publish_stream權限? – codeymcgoo

    相關問題