gpt4 book ai didi

java - 如何使用谷歌 Oauth?我在使用 Android 时收到连接失败错误

转载 作者:太空宇宙 更新时间:2023-11-04 13:12:09 26 4
gpt4 key购买 nike

我已经获得了一个 android api key 并将其放入我的 Manifest 文件中。我还使用谷歌开发者控制台获取了我的 clientID 和 SHA1 指纹。我不确定是否需要在项目中的某个位置包含我的客户端 ID?

我的客户类别:

public class Client {
private static final int REQUEST_OAUTH = 1;

private GoogleApiClient client;
private boolean authInProgress = false;

private Display display;

public interface Connection {
public void onConnected();
}
private Connection connection;

public Client(final Activity activity, final Connection connection, final Display display) {
this.display = display;
this.connection = connection;
client = new GoogleApiClient.Builder(activity)
.addApi(Fitness.SENSORS_API)
.addApi(Fitness.HISTORY_API)
.addApi(Fitness.RECORDING_API)
.addApi(Fitness.CONFIG_API)
.addScope(SCOPE_LOCATION_READ)
.addScope(SCOPE_ACTIVITY_READ)
.addScope(SCOPE_BODY_READ_WRITE)
.addConnectionCallbacks(
new GoogleApiClient.ConnectionCallbacks() {

@Override
public void onConnected(Bundle bundle) {
display.show("Connected");
connection.onConnected();
}

@Override
public void onConnectionSuspended(int i) {
display.show("Connection suspended");
if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
display.show("Connection lost. Cause: Network Lost.");
} else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
display.show("Connection lost. Reason: Service Disconnected");
}
}
}
)
.addOnConnectionFailedListener(
new GoogleApiClient.OnConnectionFailedListener() {
// Called whenever the API client fails to connect.
@Override
public void onConnectionFailed(ConnectionResult result) {
display.log("Connection failed. Cause: " + result.toString());
if (!result.hasResolution()) {
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), activity, 0).show();
return;
}

if (!authInProgress) {
try {
display.show("Attempting to resolve failed connection");
authInProgress = true;
result.startResolutionForResult(activity, REQUEST_OAUTH);
} catch (IntentSender.SendIntentException e) {
display.show("Exception while starting resolution activity: " + e.getMessage());
}
}
}
}
)
.build();
}

public void connect() {
client.connect();
}

public void disconnect() {
if (client.isConnected()) {
client.disconnect();
}
}

// disable should be called only for revoking authorization in GoogleFit
public void revokeAuth() {
PendingResult<Status> pendingResult = ConfigApi.disableFit(client);
}

public GoogleApiClient getClient() {
return client;
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_OAUTH) {
display.log("onActivityResult: REQUEST_OAUTH");
authInProgress = false;
if (resultCode == Activity.RESULT_OK) {
// Make sure the app is not already connected or attempting to connect
if (!client.isConnecting() && !client.isConnected()) {
display.log("onActivityResult: client.connect()");
client.connect();
}
}
}
}
}

我的 logcat 中出现错误:

11-22 08:54:34.204 14727-14727/com.svi.activitytracker D/com.svi.activitytracker.lib.Client: Connection failed. Cause: ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{52e26364: android.os.BinderProxy@52e457d0}}
11-22 08:54:34.204 14727-14727/com.svi.activitytracker D/com.svi.activitytracker.lib.Client: Attempting to resolve failed connection
11-22 08:54:38.144 14727-14727/com.svi.activitytracker D/com.svi.activitytracker.ui.MainActivity: onActivityResult
11-22 08:54:38.144 14727-14727/com.svi.activitytracker D/com.svi.activitytracker.lib.Client: onActivityResult: REQUEST_OAUTH

最佳答案

您需要创建一个 oauth2 客户端 ID 并从 api pannel 启用健身 api。您不会在那里找到它,您必须在搜索栏上方输入它。那么它将可供引用Try this

关于java - 如何使用谷歌 Oauth?我在使用 Android 时收到连接失败错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33856560/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com