我如何使用谷歌表api v4?



我试图切换到新的谷歌电子表格API v4在我的android工作室应用程序。我试图从URL加载数据,但程序找不到凭证JSON,我不知道把它放在哪里,所以程序会看到它。

我使用的代码从谷歌电子表格v4快速启动github....

错误:

W/System.err: java.io.FileNotFoundException: /credential.json (No such file or directory)

代码:

package com.example.sheetsapi;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
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.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.SheetsScopes;
import com.google.api.services.sheets.v4.model.ValueRange;
import java.io.FileInputStream;
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;
public class MainActivity extends AppCompatActivity {
private static final String APPLICATION_NAME = "Google Sheets API Java Quickstart";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final String TOKENS_DIRECTORY_PATH = "tokens";
private static final List<String> SCOPES = Collections.singletonList(SheetsScopes.SPREADSHEETS_READONLY);
private static final String CREDENTIALS_FILE_PATH = "/credential.json";
static String val = "";
TextView text;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = findViewById(R.id.txt);
btn = findViewById(R.id.btn);
try {
doShit();
} catch (IOException e) {
e.printStackTrace();
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
text.setText(val);
}
});
}
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
InputStream in = new FileInputStream(CREDENTIALS_FILE_PATH);
if (in == null) {
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
}
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
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();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
public static void doShit() throws IOException, GeneralSecurityException {
final NetHttpTransport HTTP_TRANSPORT = new com.google.api.client.http.javanet.NetHttpTransport();
final String spreadsheetId = "1HljdyhGauam-1WYa_C1ho2KOQMUrbGUuR3s5OEU8XtE";
final String range = "Sheet1!A2:A2";
Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName(APPLICATION_NAME)
.build();
ValueRange response = service.spreadsheets().values()
.get(spreadsheetId, range)
.execute();
List<List<Object>> values = response.getValues();
if (values == null || values.isEmpty()) {
val = "No data found.";
} else {
for (List row : values) {
Log.d("mss", "%s, %sn" + row.get(0) + row.get(1));
val = (String) row.get(0);
}
}
}
}

项目目录的图像:在这里输入图像描述

在项目中创建资产文件夹并添加凭据。Json(检查附件截图)文件,然后使用以下代码访问它:

InputStream in = getActivity().getAssets().open("credentials.json");
if (in == null) {
throw new FileNotFoundException("Resource not found: ");
}
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(new GsonFactory(), new InputStreamReader(in));

相关内容

  • 没有找到相关文章

最新更新