2011-04-16 73 views
3

我已經找到了如何使用圖表api代表facebook用戶向牆上發佈內容。但是現在我想以我的應用程序的名義發佈一些內容。我的Facebook應用程序如何將消息發佈到牆上?

這裏是如何我試圖做到這一點:

protected void btn_submit_Click(object sender, EventArgs e) 
{ 

    Dictionary<string, string> data = new Dictionary<string, string>(); 
    data.Add("message", "Testing"); 
    // i'll add more data later here (picture, link, ...) 
    data.Add("access_token", FbGraphApi.getAppToken()); 
    FbGraphApi.postOnWall(ConfigSettings.getFbPageId(), data); 

} 

FbGraphApi.getAppToken()

// ... 
private static string graphUrl = "https://graph.facebook.com"; 
//... 

public static string getAppToken() { 
    MyWebRequest req = new MyWebRequest(graphUrl + "/" + "oauth/access_token?type=client_cred&client_id=" + ConfigSettings.getAppID() + "&client_secret=" + ConfigSettings.getAppSecret(), "GET"); 
    return req.GetResponse().Split('=')[1]; 
} 

FbGraphApi.postOnWall()

public static void postOnWall(string id, Dictionary<string,string> args) 
    { 
     call(id, "feed", args); 
    } 

FbGraphApi.call( )

private static void call(string id, string method, Dictionary<string,string> args) 
{ 
    string data = ""; 
    foreach (KeyValuePair<string, string> arg in args) 
    { 

     data += arg.Key + "=" + arg.Value + "&"; 

    } 

    MyWebRequest req = new MyWebRequest(graphUrl +"/" + id + "/" + method, "POST", data.Substring(0, data.Length - 1)); 


    req.GetResponse(); // here i get: "The remote server returned an error: (403) Forbidden." 
} 

有沒有人看到我在哪裏出錯?我真的堅持這一點。

謝謝!

+0

您使用什麼語言? – Shaz 2011-04-16 14:56:05

+0

http://stackoverflow.com/questions/4641680/android-facebook-graph-api-to-update-status 這也會幫助你。謝謝 – SALMAN 2011-11-22 16:20:23

回答

3

您需要獲取應用程序的身份驗證令牌以作爲該應用程序發佈。

Auth_Token定義您發佈的安全上下文。

您需要爲當前用戶請求以下Graph API URL以查找您的應用程序的訪問令牌。

https://graph.facebook.com/me/accounts?access_token=XXXXXXXX 

這應該給你類似以下的輸出:

{ 
    "data": [ 
     { 
     "name": "My App", 
     "category": "Application", 
     "id": "10258853", 
    "access_token": "xxxxxxxxxxxxxxxx" 
     } 
    ] 
} 

確保您有manage_pages權限調用該API之前,或您將無法獲得訪問令牌回來。

一旦你有了訪問令牌,你就可以像任何其他用戶那樣發佈到牆上。請注意,URL中使用的ID與應用程序的ID相匹配。這將作爲應用程序發佈到應用程序的牆上。

https://graph.facebook.com/10258853/feed?access_token=XXXXXXX 

確保您在發佈到牆上之前也擁有publish_stream權限。

+0

https://graph.facebook.com/oauth/access_token?type=client_cred&client_id=[AppID]&client_secret=[AppSecret] 這是我用來獲取令牌的。這不是驗證令牌嗎? – ThdK 2011-04-16 15:13:18

+1

這將直接爲用戶提供訪問令牌。你需要一個應用程序本身。我更新了上面的答案,以便更詳細。 – 2011-04-16 15:18:02

+0

好的這個作品完美的寫一些東西到應用程序牆。但是如果我想使用我的粉絲頁面,它不起作用。我也想在頁面本身的行爲頁面上寫一些東西。我試着用你以前的答案得到的access_tokens。 – ThdK 2011-04-18 10:27:37

2

最近我和FB api一起工作過。
我已經完成了JavaScript中的每一件事。
以下是我用來發布到用戶牆上的內容。
我希望這可以幫助你。

  • 包括由FB提供的JavaScript庫並將您的應用ID添加到它。

     
    <div id="fb-root"></div> 
         <script> 
    
         window.fbAsyncInit = function() { 
         FB.init({appId: 'your app id', status: true, cookie: true, 
           xfbml: true}); 
         }; 
         (function() { 
         var e = document.createElement('script'); 
         e.type = 'text/javascript'; 
         e.src = document.location.protocol + 
          '//connect.facebook.net/en_US/all.js'; 
         e.async = true; 
         document.getElementById('fb-root').appendChild(e); 
         }()); 
    
         </script> 
    

  • 對於登錄,我用一個按鈕 「fb_login」 爲ID,然後我用的jQuery如下:
     
    $("#fb_login").click(function(){ 
        FB.login(function(response) { 
         if (response.session) 
         { 
          if (response.perms) 
          { 
               // alert("Logged in and permission granted for posting"); 
          } 
          else 
          { 
            // alert("Logged in but permission not granted for posting"); 
          } 
         } 
         else 
         { 
             //alert("Not Logged In"); 
         } 
    }, {perms:'publish_stream'}); 
    

    請注意,您必須添加{燙髮: 'publish_stream'}如上所述,這將獲得您發佈到用戶牆的權利。

  • 使用id = 「stream_publish」,然後將下面的jQuery A鍵:

     
    $("#stream_publish").click(function(){ 
    
         FB.getLoginStatus(function(response){ 
    
          if(response.session) 
          { 
           publishPost(response.session); 
          } 
    
         }); 
    }); 
    
    function publishPost(session) 
    { 
        var publish = { 
         method: 'stream.publish', 
         message: 'Your Message', 
         picture : 'Image to be displayed', 
         link : 'The link that will be the part of the post, which can point to either your app page or your personal page or any other page', 
         name: 'Name or title of the post', 
         caption: 'Caption of the Post', 
         description: 'It is fun to write Facebook App!', 
         actions : { name : 'Start Learning', link : 'link to the app'} 
        }; 
    
        FB.api('/me/feed', 'POST', publish, function(response) { 
    
         document.getElementById('confirmMsg').innerHTML = 
           'A post had just been published into the stream on your wall.'; 
        }); 
    }; 
    

  • 1
    private class FbWebViewClient extends WebViewClient { 
    
        boolean started=false; 
    
        @Override 
        public boolean shouldOverrideUrlLoading(WebView view, String url) { 
         Log.d("Facebook-WebView", "Redirect URL: " + url); 
         if (url.startsWith(Facebook.REDIRECT_URI)) { 
          Bundle values = Util.parseUrl(url); 
    
          String error = values.getString("error"); 
          if (error == null) { 
           error = values.getString("error_type"); 
          } 
    
          if (error == null) { 
           mListener.onComplete(values); 
          } else if (error.equals("access_denied") 
            || error.equals("OAuthAccessDeniedException")) { 
           mListener.onCancel(); 
          } else { 
           mListener.onFacebookError(new FacebookError(error)); 
          } 
    
          FbDialog.this.dismiss(); 
          return true; 
         } else if (url.startsWith(Facebook.CANCEL_URI)) { 
          mListener.onCancel(); 
          FbDialog.this.dismiss(); 
          return true; 
         } else if (url.contains(DISPLAY_STRING)) { 
          return false; 
         } 
         // launch non-dialog URLs in a full browser 
         getContext().startActivity(
           new Intent(Intent.ACTION_VIEW, Uri.parse(url))); 
         return true; 
        } 
    
        @Override 
        public void onReceivedError(WebView view, int errorCode, 
          String description, String failingUrl) { 
         super.onReceivedError(view, errorCode, description, failingUrl); 
         mListener.onError(new DialogError(description, errorCode, 
           failingUrl)); 
         FbDialog.this.dismiss(); 
        } 
    
        public Map<String, String> getUrlParameters(String url) 
          throws UnsupportedEncodingException { 
         Map<String, String> params = new HashMap<String, String>(); 
         String[] urlParts = url.split("\\?"); 
         if (urlParts.length > 1) { 
          String query = urlParts[1]; 
          for (String param : query.split("&")) { 
           String pair[] = param.split("="); 
           String key = URLDecoder.decode(pair[0], "UTF-8"); 
           String value = ""; 
           if (pair.length > 1) { 
            value = URLDecoder.decode(pair[1], "UTF-8"); 
           } 
    
           params.put(key, value); 
    
          } 
         } 
         return params; 
        } 
    
        @Override 
        public void onPageStarted(WebView view, String url, Bitmap favicon) { 
         Log.d("Facebook-WebView", "Webview loading URL: " + url); 
    
         String newUrl="http://www.facebook.com/dialog/feed?_path=feed&app_id="; 
         if (url.contains("touch") && started==false) { 
    
          started=true; 
          ChildTabBibleLessonActivity.fbMaterial=ChildTabBibleLessonActivity.fbMaterial.replace(" ", "+"); 
          url=url+"&picture=http://www.minibiblecollege.org/mbclandingpage/images/icmlogo-small.jpg&description="+ChildTabBibleLessonActivity.fbMaterial; 
         /* Map<String,String> param; 
          try { 
           param = getUrlParameters(url); 
           newUrl=newUrl+param.get("app_id")+"&redirect_uri="+"https://deep-rain-6015.herokuapp.com"+"&display=page&picture=http://www.minibiblecollege.org/mbclandingpage/images/icmlogo-small.jpg"+"&name=MiniBible&description=heregoesMyMessage"; 
    
          } catch (UnsupportedEncodingException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 
          */ 
          view.loadUrl(url); 
          //super.onPageStarted(view, url, favicon); 
         } 
         else 
         { 
         super.onPageStarted(view, url, favicon); 
         } 
         mSpinner.show(); 
        } 
    
        @Override 
        public void onPageFinished(WebView view, String url) { 
         super.onPageFinished(view, url); 
         mSpinner.dismiss(); 
         /* 
         * Once webview is fully loaded, set the mContent background to be 
         * transparent and make visible the 'x' image. 
         */ 
         mContent.setBackgroundColor(Color.TRANSPARENT); 
         mWebView.setVisibility(View.VISIBLE); 
         mCrossImage.setVisibility(View.VISIBLE); 
        } 
    } 
    
    +0

    渲染位於FBDialog.Class.Facebook Connect Graph Api中的FBViewClient類。感謝它一定會幫助你。 – SALMAN 2011-11-22 16:24:32

    +1

    有關如何回答問題的更多信息,請閱讀[常見問題]。我們感謝您的幫助,但我們希望鼓勵用戶添加精心設計的答案。 – Will 2011-11-22 19:15:50

    相關問題