gpt4 book ai didi

java - 发布不带元数据的 Picasa 网络相册照片(结果 403 禁止)

转载 作者:行者123 更新时间:2023-12-01 15:27:08 25 4
gpt4 key购买 nike

将没有元数据的照片上传到 Picasa 网络相册时发生错误(返回 403 Forbidden)。(备注:它使用的是 OAuth2.0,“userId”不是 gmail 地址[数字 id])我不知道场合。有人请技术支持吗谢谢

public String setdData(Context context, String filePath, String userId, String albumId, String accessToken) {

// new ImageUploader(context, filePath, filePath).run();
String url = "https://picasaweb.google.com/data/feed/api/user/" + userId + "/albumid/"
+ albumId;
HttpClient httpClient = new DefaultHttpClient();
File file = new File(filePath);
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("GData-Version", "2");
// httpPost.addHeader("MIME-version", "1.0");
httpPost.setHeader("Content-type", "image/jpeg");
httpPost.setHeader("Slug", "plz-to-love-realcat.jpg");
// httpPost.addHeader("Content-Length", String.valueOf(file.length()));
httpPost.setHeader("Authorization", "GoogleLogin auth=" + accessToken);
// httpPost.setHeader("Authorization", "OAuth " + accessToken);

InputStreamEntity reqEntity;
org.apache.http.HttpResponse response;

try {
reqEntity = new InputStreamEntity(new FileInputStream(file), file.length());

String CONTENTTYPE_BINARY = "binary/octet-stream";
reqEntity.setContentType(CONTENTTYPE_BINARY);
reqEntity.setChunked(true);
httpPost.setEntity(reqEntity);
response = httpClient.execute(httpPost);

Log.d("Picasa Upload", "STATUS CODE : " + response.getStatusLine().getStatusCode());

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

最佳答案

我假设您的 accessToken 不为空...

这是我的代码,它对我有用:

public boolean setdData(Context context, String filePath, String userId, String albumId, String accessToken) {
boolean success = false;
Bitmap bitmap = BitmapFactory.decodeFile(filePath);

if (bitmap != null) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 87, baos);
byte[] data = baos.toByteArray();

ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
HttpRequestFactory requestFactory = new NetHttpTransport().createRequestFactory();
InputStreamContent content = new InputStreamContent("image/jpeg", inputStream);

HttpRequest request = requestFactory.buildPostRequest(new genericUrl("https://picasaweb.google.com/data/feed/api/user/"+userId+"/"+albumId+"/default"), content);
GoogleHeaders headers = new GoogleHeaders();
String fileName = "Whatever...";
headers.setSlugFromFileName(fileName);
headers.setAuthorization("OAuth " + accessToken);
request.setHeaders(headers);
request.execute().ignore();
// Success!
success = true
} catch (IOException e) {
}
}
return success;
}


要使此代码正常工作,您需要包含一些库。
我用过这些:

google-http-client-1.10.2-beta.jar
google-api-client-1.10.2-beta.jar
google-oauth-client-1.10.0-beta.jar
jsr305-1.3.9.jar
Guava -11.0.1.jar

您可以在这里找到它们:
http://code.google.com/p/google-api-java-client/downloads/detail?name=google-api-java-client-1.10.2-beta.zip

关于java - 发布不带元数据的 Picasa 网络相册照片(结果 403 禁止),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10023634/

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