16

问题:在我的应用程序中,我需要像 FB 或 Twitter 一样在 Instagram 上发布图片。

我已经做了什么:登录并从 Instagram 获取照片到我自己的应用程序。但无法在 Instagram 上发布图片。

4

5 回答 5

38

然而,不可能像 FB 或 twitter 那样在 Instagram 上发布图像。

但这是使用已安装的 Instagram 实现此目的的另一种方法,如果没有,这将通知用户下载应用程序。

 public void onClick(View v) {

        Intent intent = getPackageManager().getLaunchIntentForPackage("com.instagram.android");
        if (intent != null)
        {
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.setPackage("com.instagram.android");
            try {
                shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), imagePath, "I am Happy", "Share happy !")));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            shareIntent.setType("image/jpeg");

            startActivity(shareIntent);
        }
        else
        {
            // bring user to the market to download the app.
            // or let them choose an app?
            intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setData(Uri.parse("market://details?id="+"com.instagram.android"));
            startActivity(intent);
        }

    }
于 2013-04-30T12:32:35.340 回答
13

不,你不能。引自Instagram API 文档

目前,无法通过 API 上传。我们有意识地选择不添加它,原因如下:

  1. Instagram 是关于你在旅途中的生活——我们希望鼓励从应用程序中拍摄照片。但是,将来我们可能会根据具体情况授予对单个应用程序的白名单访问权限。
  2. 我们要打击垃圾邮件和低质量的照片。一旦我们允许从其他来源上传,就很难控制进入 Instagram 生态系统的内容。尽管如此,我们正在努力确保用户在我们的平台上获得一致且高质量的体验。

更新:但是,如果您在 iOS 上工作(尽管您标记了 Android),有一种方法可以通过自定义 URL 方案“发送”照片(实际上,它会在 Instagram 中打开图像)。看到这个

于 2013-04-30T09:46:35.130 回答
0

试试这个链接:

您可以使用此类将图像上传到 Instagram。

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpCookie; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.Date; import java.util.List; import java.util.Map; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import javax.net.ssl.HttpsURLConnection; import org.apache.commons.codec.binary.Hex; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; public class InstagramPostHelper { private static InstagramPostHelper instance = null; protected InstagramPostHelper() {} public static InstagramPostHelper getInstance() { if (instance == null) { instance = new InstagramPostHelper(); } return instance; } private String GenerateSignature(String value, String key) { String result = null; try { byte[] keyBytes = key.getBytes(); SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(signingKey); byte[] rawHmac = mac.doFinal(value.getBytes()); byte[] hexBytes = new Hex().encode(rawHmac); result = new String(hexBytes, "UTF-8"); } catch (Exception e) { } return result; } private static final String COOKIES_HEADER = "Set-Cookie"; public static java.net.CookieManager msCookieManager = new java.net.CookieManager(); private HttpsURLConnection conn; private static String TextUtilsJoin(CharSequence delimiter, List<HttpCookie> list) { StringBuilder sb = new StringBuilder(); boolean firstTime = true; for (Object token: list) { if (token.toString().trim().length()<1) continue; if (firstTime) { firstTime = false; } else { sb.append(delimiter + " "); } sb.append(token); } return sb.toString(); } private String GetJSONStringAndPostData(String jsonaddress,String postdata,String agent) { String sonuc = ""; try { byte[] postDataBytes = postdata.toString().getBytes("UTF-8"); URL url = new URL(jsonaddress); conn = (HttpsURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); conn.setRequestProperty("User-Agent", agent); //Set Cookies if(msCookieManager.getCookieStore().getCookies().size() > 0) { conn.setRequestProperty("Cookie", TextUtilsJoin(";", msCookieManager.getCookieStore().getCookies())); } conn.setDoOutput(true); conn.getOutputStream().write(postDataBytes); if (conn.getResponseCode() != 200) { throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); } //Get Cookies Map<String, List<String>> headerFields = conn.getHeaderFields(); List<String> cookiesHeader = headerFields.get(COOKIES_HEADER); if(cookiesHeader != null) { for (String cookie : cookiesHeader) { msCookieManager.getCookieStore().add(null,HttpCookie.parse(cookie).get(0)); } } BufferedReader br = new BufferedReader(new InputStreamReader( (conn.getInputStream()))); String output; while ((output = br.readLine()) != null) { sonuc += output; } conn.disconnect(); } catch (MalformedURLException e) { return ""; } catch (IOException e) { return ""; } return sonuc; } public void SendImage(String Caption,byte[] ImageByteArray) throws UnsupportedEncodingException, ParseException { String Agent = "Instagram 6.21.2 Android (19/4.4.2; 480dpi; 1152x1920; Meizu; MX4; mx4; mt6595; en_US)"; String Guid = java.util.UUID.randomUUID().toString(); String DeviceId = "android-" + Guid; String Data = "{\"device_id\":\"" + DeviceId + "\",\"guid\":\"" + Guid + "\",\"username\":\"MYUSERNAME\",\"password\":\"MYPASSWORD\",\"Content-Type\":\"application/x-www-form-urlencoded; charset=UTF-8\"}"; String Sig = GenerateSignature(Data, "25eace5393646842f0d0c3fb2ac7d3cfa15c052436ee86b5406a8433f54d24a5"); Data = "signed_body=" + Sig + "." + URLEncoder.encode(Data, "UTF-8") + "&ig_sig_key_version=4"; if (msCookieManager.getCookieStore()!= null) { msCookieManager.getCookieStore().removeAll(); } //Login Request String login = GetJSONStringAndPostData("https://instagram.com/api/v1/accounts/login/", Data,Agent); JSONParser parser = new JSONParser(); Object obj = parser.parse(login); JSONObject jsonObject = (JSONObject) obj; if (((String) jsonObject.get("status")).equals("ok")) //Login SuccessFul { //Login Successful System.out.println("Login Successful !"); //Post Image String upload = ""; try { final HttpMultipartHelper http = new HttpMultipartHelper(new URL("https://instagram.com/api/v1/media/upload/")); http.addFormField("device_timestamp", Long.toString((new Date()).getTime())); http.addFilePart("photo", ImageByteArray); final byte[] bytes = http.finish(); upload = new String(bytes); } catch (IOException e) { e.printStackTrace(); } System.out.println(upload); obj = parser.parse(upload); jsonObject = (JSONObject) obj; if (((String) jsonObject.get("status")).equals("ok")) //Login SuccessFul { String mediaID = (String) jsonObject.get("media_id"); Data = "{\"device_id\":\"" + DeviceId + "\",\"guid\":\"" + Guid + "\",\"media_id\":\"" + mediaID + "\",\"caption\":\"" + Caption + "\",\"device_timestamp\":\"" + Long.toString((new Date()).getTime()).substring(0,10) + "\",\"source_type\":\"5\",\"filter_type\":\"0\",\"extra\":\"{}\",\"Content-Type\":\"application/x-www-form-urlencoded; charset=UTF-8\"}"; Sig = GenerateSignature(Data, "25eace5393646842f0d0c3fb2ac7d3cfa15c052436ee86b5406a8433f54d24a5"); Data = "signed_body=" + Sig + "." + URLEncoder.encode(Data, "UTF-8") + "&ig_sig_key_version=6"; //Login Request System.out.println(Data); String result = GetJSONStringAndPostData("https://instagram.com/api/v1/media/configure/", Data,Agent); System.out.println(result); } } else //Login UnsuccessFul { System.out.println("Login Unsuccessful !" + login); } } }

https://gist.github.com/ecundar/d5b6bdcc2035448fc9cd

于 2015-08-22T08:47:21.640 回答
0

看来我们现在可以做到了,更多你可以在这里查看官方文档

于 2018-10-08T21:00:09.390 回答
0

试试这个

public void ShareInsta() {



    File dir = new File(Environment.getExternalStorageDirectory(), "FolderName");
    File imgFile = new File(dir, "0.png");
    Intent sendIntent = new Intent(Intent.ACTION_VIEW);
    sendIntent.setType("image/*");
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + imgFile));
    sendIntent.putExtra(Intent.EXTRA_TEXT, "<---MY TEXT--->.");
    sendIntent.setPackage("com.instagram.android");
    sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    try {
        startActivity(Intent.createChooser(sendIntent, "Share images..."));
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(SaveAndShareActivity.this, "Please Install Instagram", Toast.LENGTH_LONG).show();
    }

}
于 2019-03-18T08:10:59.143 回答