Google api 为什么我在使用Gmail服务发送电子邮件时会遇到IOException?

Google api 为什么我在使用Gmail服务发送电子邮件时会遇到IOException?,google-api,google-oauth,gmail-api,Google Api,Google Oauth,Gmail Api,在我的android测试应用程序中,在我从Google开发者控制台获得JSON文件后,我在Gmail API上设置了JSON文件,并将其放在模拟器中,我得到一个IOException,它说: “com.google.api.client.googleapis.json.googlejson响应异常: 403禁止{“代码”:403,“错误”:[{ “域”:“usageLimits”, “消息”:“未配置访问权限。请使用Google开发者控制台为您的项目激活API。”, “原因”:“accessNo

在我的android测试应用程序中,在我从Google开发者控制台获得JSON文件后,我在Gmail API上设置了JSON文件,并将其放在模拟器中,我得到一个IOException,它说:

“com.google.api.client.googleapis.json.googlejson响应异常: 403禁止{“代码”:403,“错误”:[{ “域”:“usageLimits”, “消息”:“未配置访问权限。请使用Google开发者控制台为您的项目激活API。”, “原因”:“accessNotConfigured”}],“消息”:“Access未配置。请使用Google开发者控制台激活API。” 用于您的项目。“}”

我想我必须使用GoogleClientSecrets对象,但我还没有找到它的用途

代码如下:

public class MainActivity extends Activity 
{   
    final String SCOPE = "oauth2:https://www.googleapis.com/auth/gmail.compose";
    final String FILE_NAME = "TestEmail5.json";
    private static final int REQUEST_RESOLVE_ERROR = 1001;

    Button button;

    OnClickListener sendListener = new OnClickListener()
    {

        @Override
        public void onClick(View v) 
        {
            new sendEmailTask().execute();

        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(sendListener);


    }


    public static MimeMessage createEmail(String to, String from, String subject, String bodyText) throws MessagingException 
    {
            Properties props = new Properties();
            Session session = Session.getDefaultInstance(props, null);

            MimeMessage email = new MimeMessage(session);
            InternetAddress tAddress = new InternetAddress(to);
            InternetAddress fAddress = new InternetAddress(from);

            email.setFrom(new InternetAddress(from));
            email.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));
            email.setSubject(subject);
            email.setText(bodyText);

            return email;
    }


    public static void sendMessage(Gmail service, String userId, MimeMessage email) throws MessagingException, IOException
    {
            Message message = createMessageWithEmail(email);
            message = service.users().messages().send(userId, message).execute();

            System.out.println("Message id: " + message.getId());
            System.out.println(message.toPrettyString());
    }


    public static Message createMessageWithEmail(MimeMessage email) throws MessagingException, IOException 
    {
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            email.writeTo(bytes);
            String encodedEmail = Base64.encodeBase64URLSafeString(bytes.toByteArray());
            Message message = new Message();
            message.setRaw(encodedEmail);
            return message;
    }


    public class sendEmailTask extends AsyncTask
    {

        @Override
        protected Object doInBackground(Object... params)
        {
            HttpTransport httpTransport = new NetHttpTransport();
            JsonFactory jsonFactory = new JacksonFactory();
            String token = "";

            AccountManager accountManager = AccountManager.get(MainActivity.this);
            Account account[] = accountManager.getAccountsByType("com.google");

            String accountName = account[0].name;

            try 
            {
                //GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(jsonFactory,  new java.io.FileReader(Environment.getExternalStorageDirectory().getAbsolutePath() + "//" + "JSON/" + FILE_NAME));


                token = GoogleAuthUtil.getToken(MainActivity.this, accountName, SCOPE);

                GoogleCredential credential = new GoogleCredential().setAccessToken(token);

                Gmail service =  new Gmail.Builder(httpTransport, jsonFactory, credential).setApplicationName("TestEmail5").build();

                MimeMessage mm = createEmail("myemail", "myemail", "soggetto", "oggetto");

                sendMessage(service, "myemail", mm);

            } 
            catch (UserRecoverableAuthException e) 
            {
                startActivityForResult(e.getIntent(), REQUEST_RESOLVE_ERROR);
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            } 
            catch (GoogleAuthException e) 
            {
                e.printStackTrace();
            } 
            catch (MessagingException e)
            {
                e.printStackTrace();
            }


            return null;
        }

    };







}
您需要登录并创建一个项目,并为其激活“Gmail”API

更多信息,请访问:

我不能使用web视图进行身份验证,在我的最后一个项目中,必须在没有用户交互的情况下在后台进行身份验证。您用于创建“项目”并设置oauth2的开发人员帐户需要完成此操作。代码看起来很好,就像您如何进行授权一样,您只是还没有完全设置项目(oauth2开发人员密钥)。那么json没有用吗?client_secrets.json?您仍然需要它,无论如何,您还没有在web浏览器中一次性完成开发人员项目的设置,您需要在使用API之前完成此操作。我链接到的文档应该会解释这一切。更多详细信息?您是否能够通过web浏览器中的开发者控制台激活Gmail API?如果是这样,那么当运行相同的代码时会发生什么?