14

私は新しいAPIがどのように機能するかを理解しようとしているので、概念を理解することができ、Facebookにログオンし、ログアウトして画像をアップロードしようとする独自の「Hello World」を行うことができました.

HelloFacebookSample と SessionLoginSimple の両方で、facebook への簡単なログインができます。HelloFacebookSample では、[写真を投稿] をクリックすると、権限が設定されていないため、ログイン用の新しいダイアログが表示されます。

これは、HelloFacebookSample のコードです。

 private void performPublish(PendingAction action) {
    Session session = Session.getActiveSession();
    if (session != null) {
        pendingAction = action;
        if (session.getPermissions().contains("publish_actions")) {
            // We can do the action right away.
            handlePendingAction();
        } else {
            // We need to reauthorize, then complete the action when we get called back.
            Session.ReauthorizeRequest reauthRequest = new Session.ReauthorizeRequest(this, PERMISSIONS).
                    setRequestCode(REAUTHORIZE_ACTIVITY).
                    setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
            session.reauthorizeForPublish(reauthRequest);
        }
    }
}

「publish_actions」がないため、再認証する必要があります。

SimpleSessionLogin では、ログインは次のようになります。

private void onClickLogin() {
    Session session = Session.getActiveSession();
    if (!session.isOpened() && !session.isClosed()) {
        session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
    } else {
        Session.openActiveSession(this, true, statusCallback);
    }
}

そのため、ログイン中にアクセス許可を要求する方法がわかりません。まったく同じダイアログを使用して数秒間に 2 回サービスにログインするのは、奇妙であるか、冗長であると言えます。

ユーザーには、何かがおかしい、または奇妙に聞こえる場合があります。

必要なアクセス許可を使用して facebook に一度ログインしたいのですが、API 3.0 セッション呼び出しを使用しており、廃止されたものではありません。

誰でも方法を説明できますか?

4

5 に答える 5

8

Session.OpenRequestには、権限を設定できるメソッドがあります。ただし、3.0 SDK では、Facebook は現在、開発者に「読み取り」と「公開」のアクセス許可を別々に要求するよう要求しています。そのため、アプリがユーザー情報の読み取りとユーザーに代わって発行する必要がある場合は、再承認を呼び出す必要があります。

于 2012-10-24T17:00:32.873 に答える
6

これが、Facebookの3.0 SDKforAndroidを使用して作成したダイアログフラグメントです。カプセル化されているため、Facebookのすべての機能がフラグメントに含まれています。これはダイアログフラグメントであるため、実行中のアクティビティの上にポップアップ表示されます。このフラグメントを使用すると、ログインでき、Facebookに含まれているUiHelperクラスを使用してログインすると、特定のビューが有効になります。

public class FFragment extends DialogFragment {

private static final String TAG = "FacebookFragment";
private UiLifecycleHelper uiHelper;
private String mFilePath;
private Button shareButton, cancelButton;
private EditText mMessageText;
private TextView mMessageTitle;
private ProgressBar pBar;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.facebook_login, container, false);
    LoginButton authButton = (LoginButton) view
            .findViewById(R.id.authButton);
    authButton.setFragment(this);
    authButton.setPublishPermissions(Arrays.asList("publish_stream"));

    shareButton = (Button) view.findViewById(R.id.shareButton);
    mMessageText = (EditText) view.findViewById(R.id.facebook_post_text);
    mMessageTitle = (TextView) view.findViewById(R.id.facebook_post_title);
    cancelButton = (Button) view.findViewById(R.id.cancelButton);
    pBar = (ProgressBar) view.findViewById(R.id.facebook_pbar);
    cancelButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            FFragment.this.dismiss();

        }

    });
    shareButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            publishPhoto();

        }

    });
    return view;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mFilePath = getArguments().getString("file_path");

    uiHelper = new UiLifecycleHelper(getActivity(), callback);
    uiHelper.onCreate(savedInstanceState);

}

/**
 * After user selects to upload photo
 */
private void publishPhoto() {
    pBar.setVisibility(View.VISIBLE);
    GraphObject graphObject;
    Bitmap bmap = BitmapFactory.decodeFile(mFilePath);

    ByteArrayOutputStream stream = new ByteArrayOutputStream();

    bmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

    byte[] byteArray = stream.toByteArray();

    Bundle params = new Bundle();

    params.putByteArray("picture", byteArray);
    params.putString("message", mMessageText.getText() + " " + 
            getActivity().getResources().getString("String goes here"));

    Request request = new Request(Session.getActiveSession(), "me/photos",params, 
             HttpMethod.POST);

    request.setCallback(new Request.Callback() {

        @Override
        public void onCompleted(Response response) {
            if (response.getError()==null) {
                Toast.makeText(getActivity(), "Successfully posted photo", Toast.LENGTH_SHORT).show();
                FlurryAgent.logEvent(Globals.EVENT_FACEBOOK);
            } else {
                Toast.makeText(getActivity(), response.getError().getErrorMessage(), Toast.LENGTH_SHORT).show();
            }
            pBar.setVisibility(View.GONE);
            FFragment.this.dismiss();

        }
    });
    request.executeAsync();




}
private void onSessionStateChange(Session session, SessionState state,
        Exception exception) {
    if (state.isOpened()) {
        Log.i(TAG, "Logged in...");
        // Check for reading user_photos permission  
        shareButton.setVisibility(View.VISIBLE);
        mMessageText.setVisibility(View.VISIBLE);
        mMessageTitle.setVisibility(View.VISIBLE);


    } else if (state.isClosed()) {
        Log.i(TAG, "Logged out...");
        shareButton.setVisibility(View.GONE);
        mMessageText.setVisibility(View.GONE);
        mMessageTitle.setVisibility(View.GONE);
    }

}


private Session.StatusCallback callback = new Session.StatusCallback() {
    @Override
    public void call(Session session, SessionState state,
            Exception exception) {
        onSessionStateChange(session, state, exception);

    }
};

@Override
public void onResume() {
    super.onResume();
    uiHelper.onResume();
    // For scenarios where the main activity is launched and user
    // session is not null, the session state change notification
    // may not be triggered. Trigger it if it's open/closed.
    Session session = Session.getActiveSession();
    if (session != null && (session.isOpened() || session.isClosed())) {
        onSessionStateChange(session, session.getState(), null);
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    uiHelper.onActivityResult(requestCode, resultCode, data);
}

@Override
public void onPause() {
    super.onPause();
    uiHelper.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    uiHelper.onDestroy();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    uiHelper.onSaveInstanceState(outState);
}

}

次のコード行は、ダイアログを表示するためにアクティビティで使用するものです。

FFragment mFacebookFragment = new FFragment();
        Bundle args = new Bundle();
        args.putString("file_path", mFilePath);
        mFacebookFragment.setArguments(args);
        mFacebookFragment.show(getSupportFragmentManager(), "tag");

とにかく、onCreateを見ると、公開権限が設定されています。デフォルトでは、フラグメントの起動時に読み取り権限がすでに設定されています。

また、マニフェストにこれがあるようにします。

<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>

ここで、@ string / app_idは、開発者サイトでfacbeookアプリケーションを作成したときに作成したアプリIDです。また、新しいsdk facebookプロジェクトをダウンロードして、ライブラリプロジェクトとして参照してください。ダイアログレイアウト用のXMLファイル。

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >



<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical" >

    <com.facebook.widget.LoginButton
        android:id="@+id/authButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_gravity="center_horizontal|top" />

    <TextView
        android:id="@+id/facebook_post_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:text="Message to post with photo"
        android:textColor="@android:color/white"
        android:textSize="18dp"
        android:textStyle="bold"
        android:visibility="gone" />

    <EditText
        android:id="@+id/facebook_post_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minLines="3"
        android:visibility="gone" />

    <Button
        android:id="@+id/shareButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:gravity="center"
        android:text="Post"
        android:textStyle="bold"
        android:visibility="gone" />

    <Button
        android:id="@+id/cancelButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Cancel"
        android:textStyle="bold" />
</LinearLayout>

<ProgressBar 
    android:id="@+id/facebook_pbar"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:visibility="gone"
    />

于 2012-12-02T05:48:32.237 に答える
4

初めてアプリを承認/ログインするときは、読み取り権限のみを要求できます。私も以前にこの新しい SDK を試していましたが、少し混乱していました。

Facebook は、OG アクションを初めて公開するときに公開権限を再承認する必要があるようにすることで、ユーザーがアプリの公開機能に自信を持てるようにすることを望んでいます。Foursquare や Instagram が OG を公開する方法を見ると、FB でアクションを共有したいことを示す Facebook チェックボックスまたはボタンがあります。それに似たものを実装するか、私が最終的に行ったことは、ユーザーがクリックして Facebook への公開アクションを有効にするように求めるアプリを承認した後にボタンを表示することです。

于 2012-10-24T22:52:26.333 に答える
2

Facebook 3.0 API は、読み取りと公開の両方を同時に要求できないため、実際には制限されています。回避策は見つかりましたが、ライブラリを変更する必要があります。

これをSession.javaに追加するだけです:

public final void openForReadAndPublish(OpenRequest openRequest) {
        open(openRequest, null);
    }

これにより、例外をスローする検証チェックがスキップされます。もちろん、すべてを手動で行う必要があるかもしれません。また、Session の validatePermissions(openRequest, authType) 呼び出しをコメント アウトすることもできます。これにより、必要なアクセス許可を渡すことができるようになります。

于 2013-01-11T00:30:07.717 に答える