2013-12-09 41 views
9

google +でメディアファイルをアップロードしようとしていますが、GoogleのコンソールでクライアントIDを作成しました。 は私がアップロード方法フォームに取得してい
attaching-media「コード」:403、「理由」:google plusのファイルをアップロードする際の「禁止」例外

authentication

私はラインで

Media result = null; 
        try { 
         result = insertRequest.execute(); 
        } catch (IOException e1) { 
         // TODO Auto-generated catch block 
         // e1.printStackTrace(); 
         Log.e("result exception", "" + e1); 
        } 

例外を例外を取得していたアプリケーションを実行します:

12-09 18:49:20.983: E/result exception(26301):com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden 
12-09 18:49:20.983: E/result exception(26301): { 
12-09 18:49:20.983: E/result exception(26301): "code" : 403, 
12-09 18:49:20.983: E/result exception(26301): "errors" : [ { 
12-09 18:49:20.983: E/result exception(26301):  "domain" : "global", 
12-09 18:49:20.983: E/result exception(26301):  "message" : "Forbidden", 
12-09 18:49:20.983: E/result exception(26301):  "reason" : "forbidden" 
12-09 18:49:20.983: E/result exception(26301): } ], 
12-09 18:49:20.983: E/result exception(26301): "message" : "Forbidden" 
12-09 18:49:20.983: E/result exception(26301): } 

と私のコードです打撃:

public class MainActivity extends Activity { 
public static final String CLIENT_ID = "***********************************"; 
public static final String CLIENT_SECRET = "******************************"; 
String REDIRECT_URI = "http://localhost"; 
// String REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob"; 
JsonFactory jsonFactory = new JacksonFactory(); 
String posturl ="https://www.googleapis.com/upload/plusDomains/v1/people/userId/media/collection"; 
HttpTransport transport = new NetHttpTransport(); 
GoogleTokenResponse tokenResponse; 
@SuppressLint({ "NewApi", "SetJavaScriptEnabled" }) 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // setContentView(R.layout.activity_main); 
    WebView webview = new WebView(this); 
    webview.setVisibility(View.VISIBLE); 
    webview.getSettings().setJavaScriptEnabled(true); 
    setContentView(webview); 

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
      .permitAll().build(); 

    StrictMode.setThreadPolicy(policy); 
    Collection<String> SCOPE = Arrays.asList(
      "https://www.googleapis.com/auth/plus.me", 
      "https://www.googleapis.com/auth/plus.media.upload", 
      "https://www.googleapis.com/auth/plus.stream.write"); 
    //  "https://www.googleapis.com/auth/plus.profiles.read"); 
    final GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
      transport, jsonFactory, CLIENT_ID, 
      CLIENT_SECRET, SCOPE).setApprovalPrompt("force") 
      .setAccessType("offline").build(); 

    String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI) 
      .build(); 
    System.out.println("url: " + url); 
    webview.loadUrl(url); 

    webview.setWebViewClient(new WebViewClient() { 

     @Override 
     public void onPageFinished(WebView view, String url) { 

      if (url.startsWith(REDIRECT_URI)) { 

       if (url.indexOf("code=") != -1) { 
      // Url is like http://localhost/?code=4/Z5DgC1IxNL-muPsrE2Sjy9zQn2pF 
        String code = url.substring(REDIRECT_URI.length() + 7, 
          url.length()); 

        System.out.println("code: " + code); 
        tokenResponse = null; 
        try { 
         tokenResponse = flow.newTokenRequest(code) 
           .setRedirectUri(REDIRECT_URI).execute(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        GoogleCredential credential = new GoogleCredential.Builder() 
          .setTransport(transport) 
          .setJsonFactory(jsonFactory) 
          .setClientSecrets(CLIENT_ID, CLIENT_SECRET) 
          .addRefreshListener(
            new CredentialRefreshListener() { 
             @Override 
             public void onTokenResponse(
               Credential credential, 
               TokenResponse tokenResponse) { 
              System.out 
                .println("Credential was refreshed successfully."); 
             } 

             @Override 
             public void onTokenErrorResponse(
               Credential credential, 
               TokenErrorResponse tokenErrorResponse) { 
              System.err 
                .println("Credential was not refreshed successfully. " 
                  + "Redirect to error page or login screen."); 
             } 
            }).build(); 

        credential.setFromTokenResponse(tokenResponse); 

        PlusDomains plusDomains = new PlusDomains.Builder(
          transport, jsonFactory, credential).setApplicationName("RiskScore").build(); 

        String userId = "me"; // Requires the plus.me scope 
        File jpegFile = null; 
         jpegFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/RiskScore/20131205115454.jpg"); 



        Media mediaDescription = new Media(); 
        mediaDescription.setDisplayName("A picture of Score"); 
        Insert insertRequest = null; 
        try { 
         insertRequest = plusDomains.media().insert(userId, 
           "cloud", mediaDescription, 
           new FileContent("image/jpeg", jpegFile)); 
        } catch (IOException e1) { 
         // TODO Auto-generated catch block 
         // e1.printStackTrace(); 
         Log.e("insertRequest exception", "" + e1); 
        } 

        Media result = null; 
        try { 
         result = insertRequest.execute(); 
        } catch (IOException e1) { 
         // TODO Auto-generated catch block 
         // e1.printStackTrace(); 
         Log.e("result exception", "" + e1); 
        } 
        if (result != null) { 
         String mediaId = result.getId(); 

         // Now create a post using the uploaded image 

         // Create the activity and populate the contents 
         com.google.api.services.plusDomains.model.Activity activity = new com.google.api.services.plusDomains.model.Activity(); 
         activity.setObject(new PlusDomainsObject()); 
         activity.getObject().setContent("Risk Project...."); 

         // Attach the photo 
         PlusDomainsObject.Attachments attachment = new PlusDomainsObject.Attachments(); 
         attachment.setObjectType("photo"); 
         attachment.setId(mediaId); 
         ArrayList<Attachments> attachments = new ArrayList<PlusDomainsObject.Attachments>(); 
         attachments.add(attachment); 
         // You can also add multiple attachments to the post 
         activity.getObject().setAttachments(attachments); 

         // Set the activity to be visible only to your domain 
         PlusDomainsAclentryResource acl = new PlusDomainsAclentryResource(); 
         acl.setType("domain"); 
         Acl aclEntries = new Acl(); 
         aclEntries 
           .setItems(new ArrayList<PlusDomainsAclentryResource>()); 
         aclEntries.getItems().add(acl); 
         aclEntries.setDomainRestricted(true); 
         // Required, this does the domain restriction 
         activity.setAccess(aclEntries); 

         // Post the activity 
         com.google.api.services.plusDomains.model.Activity newActivity = null; 
         try { 
          newActivity = plusDomains.activities() 
            .insert(userId, activity).execute(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
         System.out.println("New activity created with ID " 
           + newActivity.getId()); 
         System.out.println("URL: " + newActivity.getUrl()); 

         view.setVisibility(View.INVISIBLE); 
        } 
       } else if (url.indexOf("error=") != -1) { 
        view.setVisibility(View.INVISIBLE); 
       } 
      } 
     } 
    }); 

} 

}

+0

これはなんですか?これはシャープですか? –

+0

これはjavaです – Cropper

答えて

5

私はこのコードはサーバー側のアップロードのためのものだと思うし、Androidの携帯電話でファイルをアップロードしようとしているので、例外が表示されています。これらによって、Question1Question2は、一般の開発者が使用できる独立した書き込みAPIがないことがわかりました。これは、アプリを使用してGoogle+に投稿する唯一の方法です。

Note:一般的な開発者向けにGoogle+のAPIを知っている人がいれば、助けてください。

5

がGoogle+ドメインのAPIを使用するには、ドメインのユーザーのためのアプリのために必要な権限を設定されているの代わりに動作していることを確認する必要があります。これらの手順は、quick-start guideのステップ1の「サービスアカウントへのドメイン全体の委任を委任する」セクションの下にあります。

具体的には、アプリのクライアントIDを、アプリがドメインのコントロールパネルで使用するスコープに関連付ける必要があります。ドメイン管理者がこれを行うことができる唯一の人です。そのため、別のドメインで作業している場合は、その人と連絡を取るようにしてください。また、コントロールパネルに表示されているスコープは、アプリでリクエストしたスコープと正確に一致する必要があります。

+0

私の携帯にgoogleplusアプリがあれば、私のアプリからのメディアファイルをgoogle plusに共有できます。私はピカサの共有のようにアンドロイドのGoogle +のメディアファイルを共有するためのAPIがあることを知りたい。 – Cropper

+0

モバイルクライアントではなく、(バックエンドの)サービスアカウントを承認するためのものではありませんか?コントロールパネルには、「これらの登録されたクライアントに、ユーザーが個別に同意またはパスワードを要求することなく**あなたのユーザーデータにアクセスする権限を与えることができます**」と述べています。 –

関連する問題