gpt4 book ai didi

android - 在不使用他们的 api 的情况下将 Android 应用程序安装报告回 facebook

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:48:15 37 4
gpt4 key购买 nike

我想从我的 Android 应用程序中将其安装发布回 Facebook,以便对他们的新移动应用程序安装广告进行转化跟踪,但我想在不使用他们的 API 的情况下进行。

所以而不是做

com.facebook.Settings.publishInstall(context, appId);

我只想将带有所需参数的 HTTP 请求发送到某个 URL。

编辑:

我记录了发送到 facebook 以发布应用安装的两个请求,它们看起来像这样:

要求:

GET /[app id]?format=json&sdk=android&fields=supports_attribution HTTP/1.1

User-Agent: FBAndroidSDK.3.0.0.b

Content-Type: multipart/form-data; boundary=3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f

Host: graph.facebook.com

Connection: Keep-Alive

Accept-Encoding: gzip

响应:

HTTP/1.1 200 OK

Access-Control-Allow-Origin: *

Cache-Control: private, no-cache, no-store, must-revalidate

Content-Type: text/javascript; charset=UTF-8

ETag: "24ea6554744eece05b90dd2e65af63277cdcaf53"

Expires: Sat, 01 Jan 2000 00:00:00 GMT

Pragma: no-cache

X-FB-Rev: 658994

X-FB-Debug: P2GE3fDVAnRJh62rBS5WXD4ce1hTy8Pwvjq5rT/I+TI=

Date: Tue, 30 Oct 2012 11:37:09 GMT

Connection: keep-alive

Content-Length: 52



{"supports_attribution":true,"id":"[app id]"}

要求:

POST /[app id]/activities?format=json&sdk=android&migration_bundle=fbsdk%3A20120913 HTTP/1.1

User-Agent: FBAndroidSDK.3.0.0.b

Content-Type: multipart/form-data; boundary=3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f

Host: graph.facebook.com

Connection: Keep-Alive

Transfer-Encoding: chunked

Accept-Encoding: gzip



261

--3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f

Content-Disposition: form-data; name="format"



json

--3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f

Content-Disposition: form-data; name="sdk"



android

--3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f

Content-Disposition: form-data; name="migration_bundle"



fbsdk:20120913

--3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f

Content-Disposition: form-data; name="attribution"



ab175007-2725-464f-a111-b8b1a92bf1dd

--3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f

Content-Disposition: form-data; name="event"



MOBILE_APP_INSTALL

--3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f



0

响应:

HTTP/1.1 200 OK

Access-Control-Allow-Origin: *

Cache-Control: private, no-cache, no-store, must-revalidate

Content-Type: text/javascript; charset=UTF-8

Expires: Sat, 01 Jan 2000 00:00:00 GMT

Pragma: no-cache

X-FB-Rev: 658994

X-FB-Debug: +0GWQ4cu+tFeAg3QEuwYGx+HAt7t37itzxEYBaTZF8U=

Date: Tue, 30 Oct 2012 11:38:33 GMT

Connection: keep-alive

Content-Length: 4



true

我在我的应用程序中包含了一个精简版的 facebook api,它不能执行任何其他操作,只能发送这两个请求。我会尝试一下并报告它的工作原理。

最理想的情况是,我想从服务器而不是手机发送请求。

最佳答案

你可以通过简单的调用来做到这一点

FacebookHelper.appInstall(this.getApplicationContext(), "<com.your.package>",
"<your facebook app id>");

和下面的库。

请记住,只有安装了 facebook 应用程序,您才能跟踪应用程序 - 然后您就有了 attributionId。如果您想在服务器上发送此请求,您必须将 attributionId 发送到您的服务器。

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;

import java.io.IOException;
import java.nio.charset.Charset;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

public class FacebookHelper {

private static final Uri ATTRIBUTION_ID_CONTENT_URI =
Uri.parse("content://com.facebook.katana.provider.AttributionIdProvider");
private static final String ATTRIBUTION_ID_COLUMN_NAME = "aid";
private static final String USER_AGENT = "FBAndroidSDK/3.5.0";

private final DefaultHttpClient mHttpClient;
private final String mApplicationPackage;
private final String mApplicationId;

private FacebookHelper(String applicationPackage, String applicationId) {
checkNotNull(applicationPackage);
checkNotNull(applicationId);
mApplicationPackage = applicationPackage;
mApplicationId = applicationId;

mHttpClient = new DefaultHttpClient();
}

private static String getAttributionId(ContentResolver cr) {
checkNotNull(cr);
final String [] projection = {ATTRIBUTION_ID_COLUMN_NAME};
final Cursor cursor = cr.query(ATTRIBUTION_ID_CONTENT_URI, projection,
null, null, null);
if (cursor == null) {
return null;
}
try {
if (!cursor.moveToFirst()) {
return null;
}
final int attributionColumnIndex = cursor.getColumnIndex(ATTRIBUTION_ID_COLUMN_NAME);
if (attributionColumnIndex < 0) {
return null;
}
return cursor.getString(attributionColumnIndex);
} finally {
cursor.close();
}
}

public static void appInstall(Context applicationContext, String applicationPackage,
String applicationId) throws IOException {
checkNotNull(applicationContext);
checkNotNull(applicationPackage);
checkNotNull(applicationId);
final ContentResolver cr = applicationContext.getContentResolver();
final String attributionId = getAttributionId(cr);
if (attributionId == null) {
// we can not send anything if facebook app is not installed
return;
}
final FacebookHelper facebookHelper = new FacebookHelper(applicationPackage, applicationId);
facebookHelper.appInstall(attributionId);
}

private void appInstall(String attribution) throws IOException {
checkNotNull(attribution);
String url = String.format("https://graph.facebook.com/%s/activities", mApplicationId);
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
Charset charset = Charset.forName(HTTP.UTF_8);
entity.addPart("sdk", new StringBody("android", charset));
entity.addPart("format", new StringBody("json", charset));
entity.addPart("event", new StringBody("MOBILE_APP_INSTALL", charset));
entity.addPart("attribution", new StringBody(attribution, charset));
entity.addPart("auto_publish", new StringBody("false", charset));
entity.addPart("application_tracking_enabled", new StringBody("true", charset));
entity.addPart("migration_bundle", new StringBody("fbsdk:20130708", charset));
entity.addPart("application_package_name", new StringBody(mApplicationPackage, charset));

HttpEntityEnclosingRequestBase request = new HttpPost(url);
setupDefaultHeaders(request);
request.setEntity(entity);

HttpResponse response = getHttpClient().execute(request);
validateResponseCode(response, HttpStatus.SC_OK);
}

private void validateResponseCode(HttpResponse response,
int expectedStatusCode) throws IOException {
checkNotNull(response);
checkArgument(expectedStatusCode != HttpStatus.SC_UNPROCESSABLE_ENTITY);
checkArgument(expectedStatusCode != HttpStatus.SC_NOT_FOUND);

int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != expectedStatusCode) {
throw new IOException("Wrong response code: "+
"expected <" +expectedStatusCode +"> was <" +statusCode+">");
}
}

private HttpClient getHttpClient() {
return mHttpClient;
}

private void setupDefaultHeaders(HttpRequestBase request) {
checkNotNull(request);
request.setHeader("Accept", "application/json");
request.setHeader("User-Agent", USER_AGENT);
}
}

这个库使用 guava checkArgument 和 checkNotNull 如果你不使用 guava 你可以跳过这些行。

关于android - 在不使用他们的 api 的情况下将 Android 应用程序安装报告回 facebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13126268/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com