2011-01-09 9 views

答えて

6

これは完璧なコードではないかもしれませんが、今のところうまくいきます。あなたはより良いユーザー体験を取得するために呼び出されたときにどのようにして変更する必要があります:

//updating Status 
public void updateStatus(String accessToken){ 
    try { 
     Bundle bundle = new Bundle(); 
     bundle.putString("message", "test update"); 
     bundle.putString(Facebook.TOKEN,accessToken); 
     String response = facebook.request("me/feed",bundle,"POST"); 
     Log.d("UPDATE RESPONSE",""+response); 
    } catch (MalformedURLException e) { 
     Log.e("MALFORMED URL",""+e.getMessage()); 
    } catch (IOException e) { 
     Log.e("IOEX",""+e.getMessage()); 
    } 
} 

私はそれが役に立てば幸い:

//Implementing SSO 
    facebook.authorize(this, new String[]{"publish_stream"}, new DialogListener(){ 

     @Override 
     public void onComplete(Bundle values) { 
      updateStatus(values.getString(Facebook.TOKEN)); 
     } 

     @Override 
     public void onFacebookError(FacebookError e) { 
      Log.d("FACEBOOK ERROR","FB ERROR. MSG: "+e.getMessage()+", CAUSE: "+e.getCause()); 
     } 

     @Override 
     public void onError(DialogError e) { 
      Log.e("ERROR","AUTH ERROR. MSG: "+e.getMessage()+", CAUSE: "+e.getCause()); 
     } 

     @Override 
     public void onCancel() { 
      Log.d("CANCELLED","AUTH CANCELLED"); 
     } 
    }); 

これは、更新が機能です。

12

は、ここであなたがする予定のすべてはあなたが唯一の「publish_stream」の許可を取得する必要があり、ユーザーのステータスを更新している場合はグラフAPI

を使用して、私の完全なソリューションです。 developers.facebook.comから... "publish_stream":コンテンツ、コメント、お気に入りをユーザーのストリームやユーザーの友だちのストリームに投稿することをアプリに許可します。この許可を使用すると、いつでものユーザーのフィードにコンテンツを公開でき、オフラインアクセスは必要ありません。ただし、Facebookはユーザー主導の共有モデルを推奨しています。

ステータスを更新しようとするたびに再認証する必要がないため、これは重要です。このトリックは、キー/トークンを保存して、更新リクエストを送信することです。

ワンノート:Facebook.javaクラスの "Facebook.DEFAULT_AUTH_ACTIVITY_CODE"をプライベートからパブリックに変更しました。

私のコードには、保存されたトークンをチェックするボタンとブランクトークンを送信するボタンがあります。何らかの理由でトークンが失敗した場合に何が起こるかをテストできます。失敗し、APIが "OAuthException"を含む文字列を返す場合、このコードは新しい承認を試み、ステータスを再度更新しようとします。

FBTest.java

package com.test.FBTest; 

import java.io.IOException; 
import java.net.MalformedURLException; 

import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.facebook.android.DialogError; 
import com.facebook.android.Facebook; 
import com.facebook.android.FacebookError; 
import com.facebook.android.Facebook.DialogListener; 

public class FBTest extends Activity { 

Facebook facebook = new Facebook("199064386804603"); 
EditText et1; 
TextView tv1; 
Button button1; 
Button button2; 

private int mAuthAttempts = 0; 
private String mFacebookToken; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    button1 = (Button)findViewById(R.id.Button1); 
    button2 = (Button)findViewById(R.id.Button2); 
    tv1 = (TextView)findViewById(R.id.TextView1); 
    et1 = (EditText)findViewById(R.id.EditText1); 

    button1.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      b1Click(); 
     } 

    }); 

    button2.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      b2Click(); 
     } 

    }); 
} 

private void saveFBToken(String token, long tokenExpires){ 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    prefs.edit().putString("FacebookToken", token).commit(); 
} 

private void fbAuthAndPost(final String message){ 

    facebook.authorize(this, new String[]{"publish_stream"}, new DialogListener() { 

     @Override 
     public void onComplete(Bundle values) { 
      Log.d(this.getClass().getName(),"Facebook.authorize Complete: "); 
      saveFBToken(facebook.getAccessToken(), facebook.getAccessExpires()); 
      updateStatus(values.getString(Facebook.TOKEN), message); 
     } 

     @Override 
     public void onFacebookError(FacebookError error) { 
      Log.d(this.getClass().getName(),"Facebook.authorize Error: "+error.toString()); 
     } 

     @Override 
     public void onError(DialogError e) { 
      Log.d(this.getClass().getName(),"Facebook.authorize DialogError: "+e.toString()); 
     } 

     @Override 
     public void onCancel() { 
      Log.d(this.getClass().getName(),"Facebook authorization canceled"); 
     } 
    }); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode,Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    switch (requestCode){ 
    case Facebook.DEFAULT_AUTH_ACTIVITY_CODE: 
     facebook.authorizeCallback(requestCode, resultCode, data); 
    } 
} 

private void b1Click(){ 

    mAuthAttempts = 0; 

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    mFacebookToken = prefs.getString("FacebookToken", ""); 

    if(mFacebookToken.equals("")){ 
     fbAuthAndPost(et1.getText().toString()); 
    }else{ 
     updateStatus(mFacebookToken,et1.getText().toString()); 
    } 

} 

private void b2Click(){ 
    mAuthAttempts = 0; 
    updateStatus("",et1.getText().toString()); 
} 

public void updateStatus(String accessToken, String message){ 

    try {   
     Bundle bundle = new Bundle(); 
     bundle.putString("message", message);   
     bundle.putString(Facebook.TOKEN,accessToken);   
     String response = facebook.request("me/feed",bundle,"POST");   
     Log.d("UPDATE RESPONSE",""+response); 
     showToast("Update process complete. Respose:"+response); 
     if(response.indexOf("OAuthException") > -1){ 
      if(mAuthAttempts==0){ 
       mAuthAttempts++; 
       fbAuthAndPost(message); 
      }else{ 
       showToast("OAuthException:"); 
      } 
     } 
    } catch (MalformedURLException e) {   
     Log.e("MALFORMED URL",""+e.getMessage()); 
     showToast("MalformedURLException:"+e.getMessage()); 
    } catch (IOException e) {   
     Log.e("IOEX",""+e.getMessage()); 
     showToast("IOException:"+e.getMessage()); 
    } 

    String s = facebook.getAccessToken()+"\n"; 
    s += String.valueOf(facebook.getAccessExpires())+"\n"; 
    s += "Now:"+String.valueOf(System.currentTimeMillis())+"\n"; 
    tv1.setText(s); 

} 

private void showToast(String message){ 
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show(); 
} 
} 

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <EditText 
    android:id="@+id/EditText1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 
    <Button 
    android:id="@+id/Button1" 
    android:text="Test: With Auth" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 
    <Button 
    android:id="@+id/Button2" 
    android:text="Test: Without Auth" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 
    <TextView 
    android:id="@+id/TextView1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    /> 
</LinearLayout> 
+0

これは素晴らしいと非常に有用でした!どうもありがとう! –

+0

非常に良いもの、tahnkあなたはあまりにも – Shaun

+0

パーフェクト。 Hovewerは、「このアプリを既に承認しました[OK]」というダイアログを頻繁にポップアップ表示します。それを取り除く方法はありますか? –

関連する問題