2012-01-18 212 views
0

我想從我的Android應用程序發佈圖像到Facebook牆。我正在使用Facebook的SDK。Facebook錯誤API密鑰無效

從我MainActivity當我打電話FBConnectionActivity獲取連接和ID,我得到一個空指針異常

我打電話2個方法是這樣的: -

view.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View arg0) { 
      setConnection(); 
      getID(); 
    } 
} 

FBConnectionActivity,這兩個方法被定義爲:

public void setConnection() { 
      mContext = this; 
      mFacebook = Utility.mFacebook; 
      mAsyncRunner = new AsyncFacebookRunner(mFacebook); 
    }  
    public void getID(TextView txtUserName, ProgressBar progbar) { 
      username = txtUserName; 
      pb = progbar; 
      if (isSession()) { 
        Log.d(TAG, "sessionValid"); 
        mAsyncRunner.request("me", new IDRequestListener()); 
      } else { 
        // no logged in, so relogin 
        Log.d(TAG, "sessionNOTValid, relogin"); 
        mFacebook.authorize(this, PERMS, new LoginDialogListener()); 
      } 
    }  
    public void getID(){ 

     if (isSession()) { 
       Log.d(TAG, "sessionValid"); 
       mAsyncRunner.request("me", new IDRequestListener()); 
     } else { 
       // no logged in, so relogin 
       Log.d(TAG, "sessionNOTValid, relogin"); 
       mFacebook.authorize(this, PERMS, new LoginDialogListener()); 
     } 
    } 

在呼叫到isSession(),該方法調用isSessionValid()方法總是返回false。

public boolean isSession() { 
      sharedPrefs = PreferenceManager.getDefaultSharedPreferences(mContext); 
      String access_token = sharedPrefs.getString("access_token", "x"); 
      Long expires = sharedPrefs.getLong("access_expires", -1); 
      Log.d(TAG, access_token); 

      if (access_token != null && expires != -1) { 
       Log.e("isSession() method ", "acess_token : " + access_token); 
        mFacebook.setAccessToken(access_token); 
        mFacebook.setAccessExpires(expires); 
      } 
      return mFacebook.isSessionValid(); 
    } 

public boolean isSessionValid() { 
     return (getAccessToken() != null) && 
       ((getAccessExpires() == 0) || 
         (System.currentTimeMillis() < getAccessExpires())); 
} 

如果我的會話無效,如何獲取新的有效訪問令牌。每當我嘗試從我的應用程序上傳圖像時,方法isSessionValid()都會返回false。此外,我收到以下錯誤: -

01-18 12:46:35.928: D/Facebook-Example(28459): Response: {"error_code":101,"error_msg":"Invalid API key","request_args":[{"key":"method","value":"photos.upload"},{"key":"format","value":"json"}]} 
01-18 12:46:35.948: W/Facebook-Example(28459): Facebook Error: Invalid API key 

請幫幫我。

我使用以下方法在Facebook上發佈圖像。這裏有什麼不對嗎?

public void postImageonWall() { 
     Log.e("BrowsePictureActivity ", "Inside postImageonWall method"); 
     byte[] data = null; 

     Bitmap bi = BitmapFactory.decodeFile(selectedImagePath); 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     bi.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
     data = baos.toByteArray(); 

     Log.d("BrowsePictureActivity ", "data.length : " + data.length); 

     Bundle params = new Bundle(); 

    params.putString("method", "photos.upload"); 
    params.putByteArray("picture", data); 
    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook); 
    mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), 
      null); 
} 

回答

0

您還沒有貼出你的代碼片段,包括髮布圖像的Facebook牆上

If you map wrong Key and Value Pair you will get Invalid API Key. 

所以確保你使用正確的鍵和值對郵政形象,而你一起添加附件要小心與圖像。

嘗試此發佈圖片至Facebook牆:

private void postToWall(String accessToken) { 
    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook); 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bm.compress(Bitmap.CompressFormat.PNG, 100, stream); // bm is bitmap object of your image 
    byte[] byteArray = stream.toByteArray(); 
    Bundle param = new Bundle(); 
    param = new Bundle(); 
    param.putString("message", "All"); 
    param.putString("filename", "Test"); 
    param.putByteArray("image", byteArray); 
    mAsyncRunner.request("me/photos", param, "POST", new fbRequestListener(), null); 

} 

public class fbRequestListener implements RequestListener { 
    @Override 
    public void onComplete(String response, Object state) { 
    } 
    @Override 
    public void onIOException(IOException e, Object state) { 
    } 
    @Override 
    public void onFileNotFoundException(FileNotFoundException e,Object state) { 
    } 

    @Override 
    public void onMalformedURLException(MalformedURLException e,Object state) { 
    } 
    @Override 
    public void onFacebookError(FacebookError e, Object state) { 
    } 
} 
+0

我使用下面的方法對在Facebook上張貼的圖像。這裏有什麼不對嗎? –

+0

嘗試上面的片段伴侶。 – Venky

+0

@Swati Rawat如果答案是有用的,只要接受它。 – Venky

相關問題