繁体   English   中英

我可以用这个凭证 gmail api oauth2 错误做什么?

[英]what can I do with this credentials gmail api oauth2 error?

我正在尝试使用 oauth2 访问我的电子邮件并与帐户和桌面 api 进行交互-接收和发送电子邮件-我在 java 中使用了带有 gradle 的示例https://developers.google.com/gmail/api/quickstart/ java ,我得到这个错误:

线程“主”java.io.FileNotFoundException 中的异常:找不到资源:C:/Users/xxx/Documents/tableaux dir/prjet 电子邮件 gmail/src/main/ressources/credential.json 在 GmailQuickstart.getCredentials(GmailQuickstart.java: 50) 在 GmailQuickstart.main(GmailQuickstart.java:69)

请问我能做什么?

编码

    import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.GmailScopes;
import com.google.api.services.gmail.model.Label;
import com.google.api.services.gmail.model.ListLabelsResponse;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;

/* class to demonstrate use of Gmail list labels API */
public class GmailQuickstart {
    /** Application name. */
    private static final String APPLICATION_NAME = "Gmail API Java Quickstart";
    /** Global instance of the JSON factory. */
    private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
    /** Directory to store authorization tokens for this application. */
    private static final String TOKENS_DIRECTORY_PATH = "tokens";

    /**
     * Global instance of the scopes required by this quickstart.
     * If modifying these scopes, delete your previously saved tokens/ folder.
     */
    private static final List<String> SCOPES = Collections.singletonList(GmailScopes.GMAIL_LABELS);
    private static final String CREDENTIALS_FILE_PATH = "C:/Users/xxx/Documents/tableaux dir/prjet email gmail/src/main/ressources/credential.json";

    /**
     * Creates an authorized Credential object.
     * @param HTTP_TRANSPORT The network HTTP Transport.
     * @return An authorized Credential object.
     * @throws IOException If the credentials.json file cannot be found.
     */
    private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
        // Load client secrets.
        InputStream in = GmailQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
        if (in == null) {
            throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
        }
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

        // Build flow and trigger user authorization request.
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build();
        LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
        Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
        //returns an authorized Credential object.
        return credential;
    }

    public static void main(String... args) throws IOException, GeneralSecurityException {
        // Build a new authorized API client service.
        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        Gmail service = new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
                .setApplicationName(APPLICATION_NAME)
                .build();

        // Print the labels in the user's account.
        String user = "me";
        ListLabelsResponse listResponse = service.users().labels().list(user).execute();
        List<Label> labels = listResponse.getLabels();
        if (labels.isEmpty()) {
            System.out.println("No labels found.");
        } else {
            System.out.println("Labels:");
            for (Label label : labels) {
                System.out.printf("- %s\n", label.getName());
            }
        }
    }
}

转到谷歌云控制台并创建一个 cedetials.json 文件,确保为已安装/桌面或本机应用程序创建它。

它会提示您下载一个 json 文件。 将此文件放在由 CREDENTIALS_FILE_PATH 表示的目录中。

在您的情况下,文件应该使用此名称在此处,这实际上不是路径,而是文件的全名。 因此,请确保您没有为它使用不同的名称或最后没有额外的 .json。

"C:/Users/xxx/Documents/tableaux dir/prjet email gmail/src/main/ressources/credential.json"

您缺少凭据文件。 在您提供的链接中,它说这是一个先决条件。 创建它并填写值或从该链接下载并将其粘贴到位置C:/Users/xxx/Documents/tableaux dir/prjet email gmail/src/main/ressources然后将值更改为您的设置。

此页面可以帮助创建您的凭据https://developers.google.com/workspace/guides/create-credentials

正如这里所说, getResourceAsStream() 返回 null。 属性文件未加载“GmailQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH)”函数使用类路径,因此需要相对路径。 所以 String CREDENTIALS_FILE_PATH = "credentials.json" 就足够了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM