【问题标题】:using the fb graph api, image upload to an album from android app使用 fb graph api,将图像从 android 应用上传到相册
【发布时间】:2012-04-15 23:53:28
【问题描述】:

我在这个post看到如果你想创建一个新专辑并在其中发布一张图片,那么.php代码是:

 <?php

       $app_id = "YOUR_APP_ID";
       $app_secret = "YOUR_APP_SECRET";
       $post_login_url = "YOUR_POST-LOGIN_URL";
       $album_name = 'YOUR_ALBUM_NAME';
       $album_description = 'YOUR_ALBUM_DESCRIPTION';

       $code = $_REQUEST["code"];

       //Obtain the access_token with publish_stream permission 
       if(empty($code))
         {
           $dialog_url= "http://www.facebook.com/dialog/oauth?"
           . "client_id=" . $app_id 
           . "&redirect_uri=" . urlencode($post_login_url)
           . "&scope=publish_stream";
           echo("<script>top.location.href='" . $dialog_url . 
           "'</script>");
       } 
       else {
         $token_url= "https://graph.facebook.com/oauth/"
         . "access_token?"
         . "client_id=" .  $app_id 
         . "&redirect_uri=" . urlencode( $post_login_url)
         . "&client_secret=" . $app_secret
         . "&code=" . $code;
         $response = file_get_contents($token_url);
         $params = null;
         parse_str($response, $params);
         $access_token = $params['access_token'];

         // Create a new album
         $graph_url = "https://graph.facebook.com/me/albums?"
         . "access_token=". $access_token;

         $postdata = http_build_query(
         array(
          'name' => $album_name,
          'message' => $album_description
            )
          );
         $opts = array('http' =>
         array(
          'method'=> 'POST',
          'header'=>
            'Content-type: application/x-www-form-urlencoded',
          'content' => $postdata
          )
         );
         $context  = stream_context_create($opts);
         $result = json_decode(file_get_contents($graph_url, false, 
           $context));

         // Get the new album ID
         $album_id = $result->id;

         //Show photo upload form and post to the Graph URL
         $graph_url = "https://graph.facebook.com/". $album_id
           . "/photos?access_token=" . $access_token;
         echo '<html><body>';
         echo '<form enctype="multipart/form-data" action="'
         .$graph_url. ' "method="POST">';
         echo 'Adding photo to album: ' . $album_name .'<br/><br/>';
         echo 'Please choose a photo: ';
         echo '<input name="source" type="file"><br/><br/>';
         echo 'Say something about this photo: ';
         echo '<input name="message" type="text"
            value=""><br/><br/>';
         echo '<input type="submit" value="Upload" /><br/>';
         echo '</form>';
         echo '</body></html>';
      }
 ?>

我的问题是:该代码应该放在哪里?我怎么称呼它? 我想要的是从我的 android 应用程序发布到相册。还是这段代码只是为了将 fb 与我的网站集成?

【问题讨论】:

    标签: php android facebook-graph-api


    【解决方案1】:

    此代码将帮助您使用图形 api 将图片上传到 facebook。

       AsyncFacebookRunner mAsyncFbRunner = new AsyncFacebookRunner(mFacebook);
                Bundle params = new Bundle();
                //convert to byte stream
                System.out.println(selectedImagePath);
                FileInputStream is = new FileInputStream(new File(selectedImagePath));
                ByteArrayOutputStream bs = new ByteArrayOutputStream();
    
                int data = 0;
                while((data = is.read()) != -1)
                   bs.write(data);
    
                is.close();
                byte[] raw = bs.toByteArray();
                bs.close();
    
                 params.putByteArray("picture", raw);
                 params.putString("message", review );
                 mAsyncFbRunner.request("me/photos", params, "POST", new WallPostListener());
    

    【讨论】:

    • 好的,如果我想发给其他人?我换了我/照片?
    猜你喜欢
    • 2013-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多