Facebook SDK failed to generate preview for user only when opengraphobject.setproperty(“og:url”) is set

后端 未结 2 1784
-上瘾入骨i
-上瘾入骨i 2021-01-14 23:47

I have an app where I want to share a history in facebook. I don\'t have a web for my application. I only want to share an image with title and description and if it\'s posi

相关标签:
2条回答
  • 2021-01-15 00:29

    With this code it works and I don't have "Failed to generate preview". But this code is working something different that I want. This code open a OGActiondialog but when I share it the post never appear in the feed and appear in the activity registry but only appear the photo, not the name or description like a "normal" sharelink (small photo to the left and titol and description to the right).

    Is it possible to do this?

    private void OpenShareDialog2(){
    
        if (canPresentOGShareDialog) {
            Session session = Session.getActiveSession();
    
            if (session != null) {
                // Check for publish permissions
                List<String> permissions = session.getPermissions();
                if (!isSubsetOf(PERMISSIONS, permissions)) {
                    pendingPublishReauthorization = true;
                    Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, PERMISSIONS);
                    session.requestNewPublishPermissions(newPermissionsRequest);
                    Log.d(TAG, "Permisos pedidos");
                    return;
                }
    
            }
    
            OpenGraphObject oplugar = OpenGraphObject.Factory.createForPost("namespace:lugar");
            oplugar.setProperty("og:type","namespace:lugar");
            oplugar.setProperty("og:title", LugarCargado.getNombre());
            oplugar.setProperty("og:description", LugarCargado.getDescripcion());
    
    
    
            //OpenGraphAction action = GraphObject.Factory.create(OpenGraphAction.class);
            OpenGraphAction action = OpenGraphAction.Factory.createForPost("namespace:publicar");
            action.setProperty("lugar", oplugar);
            action.setType("namespace:publicar");
            //action.setProperty("previewPropertyName", "lugar");
            //action.setProperty("fb:explicitly_shared", true);
            //action.setExplicitlyShared(true);
    
            boolean userGenerated = false;
            if (urifoto != null) {
                String photoUriString = urifoto.toString();
                Pair<File, Integer> fileAndMinDimemsion = getImageFileAndMinDimension();
                userGenerated = fileAndMinDimemsion.second >= USER_GENERATED_MIN_SIZE;
    
                // If we have a content: URI, we can just use that URI, otherwise we'll need to add it as an attachment.
                if (fileAndMinDimemsion != null && urifoto.getScheme().startsWith("content")) {
                    action.setImage(getImageListForAction(photoUriString, userGenerated));
                }
            }
    
            FacebookDialog.OpenGraphActionDialogBuilder builder = new FacebookDialog.OpenGraphActionDialogBuilder(this, action, "lugar");
                    //.setFragment(SelectionFragment.this);
    
            if (urifoto != null && !urifoto.getScheme().startsWith("content")) {
                builder.setImageAttachmentFilesForAction(Arrays.asList(new File(urifoto.getPath())), userGenerated);
            }
    
            uiHelper.trackPendingDialogCall(builder.build().present()); 
        }
    }
    
    private Pair<File, Integer> getImageFileAndMinDimension() {
        File photoFile = null;
        String photoUriString = urifoto.toString();
        if (photoUriString.startsWith("file://")) {
            photoFile = new File(urifoto.getPath());
        } else if (photoUriString.startsWith("content://")) {
            String [] filePath = { MediaStore.Images.Media.DATA };
            Cursor cursor = this.getContentResolver().query(urifoto, filePath, null, null, null);
            if (cursor != null) {
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePath[0]);
                String filename = cursor.getString(columnIndex);
                cursor.close();
    
                photoFile = new File(filename);
            }
        }
    
        if (photoFile != null) {
            InputStream is = null;
            try {
                is = new FileInputStream(photoFile);
    
                // We only want to get the bounds of the image, rather than load the whole thing.
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(is, null, options);
    
                return new Pair<File, Integer>(photoFile, Math.min(options.outWidth, options.outHeight));
            } catch (Exception e) {
                return null;
            } finally {
                Utility.closeQuietly(is);
            }
        }
        return null;
    }
    

    I get it more or less from scrumptious sample!

    0 讨论(0)
  • 2021-01-15 00:52

    If you don't have a web component, then do not set a url in the og object. If you set a url, facebook will try to go scrape that url, and if the OG metadata scraped from there doesn't match what you've provided, then it will fail.

    If you want to add a bitmap to your OG action, in addition to what you're currently doing, you must also add a NativeAppCallContentProvider to your manifest. See the docs here: https://developers.facebook.com/docs/reference/android/current/class/NativeAppCallContentProvider/ This is how the Facebook app can actually get the bitmaps you're trying to send.

    Lastly, have a look at the Scrumptious sample app that ships with the sdk. It has an option (if you "skip login") that creates a new OG object, with an image attachment.

    0 讨论(0)
提交回复
热议问题